最近在做的项目中需要应用在后台常驻,用于实时上传一些健康信息数据,便于后台实时查看用户的健康状况。自从Android7.0以上后台常驻实现越来越难,尤其是8.0及以上。关于保活的文章比比皆是,但是效果并不理想,关于保活的方法也就常说的哪几种,重点在于怎么组合运用。最终实现效果为:用户不主动强制杀死的话,能够一直存活(小米,华,为活体,同僚,三星)。其中三星s8,华为nova2s用户强制杀死也能存活。
类> 类LocalService:服务(){ 私人var媒体播放器:媒体播放器# 63;=零 私人var mBilder: MyBilder& # 63;=零 覆盖乐趣> 类RemoteService:服务(){ 私人var mBilder: MyBilder& # 63;=零 覆盖乐趣> @SuppressWarnings (value=https://www.yisu.com/zixun/(“不”、“弃用”)) @RequiresApi (Build.VERSION_CODES.LOLLIPOP) 类JobHandlerService: JobService () { 私人var mJobScheduler: JobScheduler& # 63;=零 覆盖乐趣> 类NotificationUtils(背景:背景):ContextWrapper(上下文){ 私人var经理:NotificationManager& # 63;=零 私人var id:字符串=上下文。packageName +“五一”=context.packageName私人var名称:字符串 私人var背景:背景=上下文 私人var频道:NotificationChannel& # 63;=零 伴星{ @SuppressLint (“StaticFieldLeak”) 私人var notificationUtils: NotificationUtils& # 63;=零 乐趣createNotification(背景:背景、标题:字符串,内容:字符串,图标:Int,意图:意图):通知# 63;{ 如果(notificationUtils==null) { notificationUtils=notificationUtils(上下文) } var通知:通知# 63;=零 如果(Build.VERSION通知=DK_INT祝辞=26){ notificationUtils& # 63; .createNotificationChannel () notificationUtils& # 63;。getChannelNotification(标题、内容、图标、意图)& # 63;.build () 其他}{ notificationUtils& # 63;。getNotification_25(标题、内容、图标、意图)& # 63;.build () } 返回通知 } } @RequiresApi (api=Build.VERSION_CODES.O) 乐趣createNotificationChannel () { 如果(频道==null) { 频道=NotificationChannel (id、名称、NotificationManager.IMPORTANCE_MIN) 频道# 63;.enableLights(假) 频道# 63;.enableVibration(假) 频道# 63;。vibrationPattern=longArrayOf (0) 频道# 63;。setSound(空,空) getManager () .createNotificationChannel(频道) } } 私人乐趣getManager (): NotificationManager { 如果(经理==null) { 经理=getSystemService NotificationManager (Context.NOTIFICATION_SERVICE) } 返回经理! ! } @RequiresApi (api=Build.VERSION_CODES.O) 乐趣getChannelNotification(标题:字符串,内容:字符串,图标:Int,意图:意图):通知。Builder {//PendingIntent。FLAG_UPDATE_CURRENT这个类型才能传值 val pendingIntent=pendingIntent。getBroadcast(上下文,0,意图,PendingIntent.FLAG_UPDATE_CURRENT) 返回通知。Builder(上下文id) .setContentTitle(标题) .setContentText(内容) .setSmallIcon(图标) .setAutoCancel(真正的) .setContentIntent (pendingIntent) } 乐趣getNotification_25(标题:字符串,内容:字符串,图标:Int,意图:意图):NotificationCompat。Builder { val pendingIntent=pendingIntent。getBroadcast(上下文,0,意图,PendingIntent.FLAG_UPDATE_CURRENT) NotificationCompat返回。Builder(上下文id) .setContentTitle(标题) .setContentText(内容) .setSmallIcon(图标) .setAutoCancel(真正的) .setVibrate (longArrayOf (0)) .setSound(空) .setLights (0, 0, 0) .setContentIntent (pendingIntent) } }
<强>使用强>
将保活的功能封装成了一个单独的库,依赖该库即可。
应用中使用:
KeepLive。startWork (KeepLive.RunMode。流氓,ForegroundNotification(“标题”、“信息”, R.mipmap。ic_launcher对象:ForegroundNotificationClickListener { 覆盖有趣foregroundNotificationClick(背景:背景下,意图:意图){//点击通知回调 } }),对象:KeepLiveService { 覆盖乐趣> & lt; !——权限配置——比; & lt; uses-permission android: name=" android.permission。FOREGROUND_SERVICE”/比; & lt; uses-permission android: name=" android.permission。GET_TASKS”/比; & lt; uses-permission android: name=" android.permission。REORDER_TASKS”/比; & lt; !——保活相关配置——比; & lt;接收机android: name=" com.xiyang51.keeplive.receiver。NotificationClickReceiver”/比; & lt;活动android: name=" com.xiyang51.keeplive.activity。OnePixelActivity”/比; & lt;服务android: name=" com.xiyang51.keeplive.service。LocalService”/比; & lt;服务android: name=" com.xiyang51.keeplive.service。HideForegroundService”/比; & lt;服务 android: name=" com.xiyang51.keeplive.service.JobHandlerService " android:许可=" android.permission。BIND_JOB_SERVICE”/比; & lt;服务 android: name=" com.xiyang51.keeplive.service.RemoteService " android:过程=":远程“/祝辞Android应用保活实践详解