博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[保活]Service 服务优先级提高,防止被查杀
阅读量:4147 次
发布时间:2019-05-25

本文共 3485 字,大约阅读时间需要 11 分钟。

1. 现象

Killing process:pid: 4967,(com.android.lava.powersave),adj=900, mem=17193KB

06-11 10:09:18.064335  4967  4967 I LavaPowerSaveService: LavaPowerSaveServicestart06-11 10:10:14.558997  6120  6120 I LavaPowerSaveService: LavaPowerSaveServicestart06-11 10:10:12.833630   795  1218 I am_proc_died: [0,4967,com.android.lava.powersave,900,11]06-11 10:10:12.834047   795  1218 I am_schedule_service_restart: [0,com.android.lava.powersave/.service.LavaPowerSaveService,1000]1,509: 06-11 10:10:12.671687   795   832 D DuraSpeed/DSPolicyFactory: Application = com.android.launcher3 (50MB), FreeMemory = 382MB 1,510: 06-11 10:10:12.743003   795   918 D DataShapingService: [openLteDataUpLinkGate] isForce: false 1,511: 06-11 10:10:12.743139   795   918 D DataShapingService: Alarm manager openLteDataUpLinkGate: false 1,512: 06-11 10:10:12.743199   795   918 I DataShapingService: openLteGateSuccess = false 1,513: 06-11 10:10:12.767639   795   832 V DuraSpeed/AWSManager: Killing process:pid: 4967,(com.android.lava.powersave),adj=900, mem=17193KB 1,514: 06-11 10:10:12.768095   795   832 V DuraSpeed/AWSManager: kill end[com.android.lava.powersave] 1,515: 06-11 10:10:12.768172   795   832 V DuraSpeed/AWSManager: suppress start [] 1,516: 06-11 10:10:12.768253   795   832 D DuraSpeed/DuraSpeedInternalManager: switchState end 1,517: 06-11 10:10:12.833457   795  1218 I ActivityManager: Process com.android.lava.powersave (pid 4967) has died: cch  SVC  1,518: 06-11 10:10:12.833921   795  1218 W ActivityManager: Scheduling restart of crashed service com.android.lava.powersave/.service.LavaPowerSaveService in 1000ms 1,519: 06-11 10:10:12.843993   795   829 W ActivityManager: setHasOverlayUi called on unknown pid: 4967 1,513: 06-11 10:10:12.767639   795   832 V DuraSpeed/AWSManager: Killing process:pid: 4967,(com.android.lava.powersave),adj=900, mem=17193KB 1,514: 06-11 10:10:12.768095   795   832 V DuraSpeed/AWSManager: kill end[com.android.lava.powersave]

缺点就是:通知栏常驻,其中7.0 只要不设置 setSmallIcon ,消息通知就可以不显示,但是 8.0 目前强制显示

2. 查看优先级

  • adb shell “ps|grep 关键字”

  • adb shell “cat /proc/进程号/oom_adj”

adb shell "ps|grep powersave"system    21920 443   1493244 105644 SyS_epoll_ 76b3e66900 S com.android.lava.powersave// 前台进程adb shell "cat /proc/21920/oom_adj"

查看发现自己进程不在前台是oom_adj在 11 以后

对应如下图

oom_adj

3. 提高进程优先级

  • Service 中调用 startForeground 进行进程优先级设置为前台进程
private void startForeground() {        Notification.Builder nb =  new Notification.Builder(mContext);        if (android.os.Build.VERSION.SDK_INT >= 26) {            String CHANNEL_ONE_ID = "channel_id_foreground";            String CHANNEL_ONE_NAME = "Channel One";            NotificationChannel notificationChannel = null;            notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_LOW);            nb.setChannelId(CHANNEL_ONE_ID);            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);            manager.createNotificationChannel(notificationChannel);        }        // 7.0 只要不设置 setSmallIcon ,消息通知就可以不显示        nb.setSmallIcon(R.drawable.ic_settings_battery);        nb.setContentTitle(mContext.getString(R.string.battery_mode_title));        nb.setContentText(mContext.getString(R.string.powersave_enable_tip));        try {            startForeground(0x88, nb.build());        } catch (Exception e) {            e.printStackTrace();        }    }}
  • Service 中调用 stopForeground(true) 取消前台进程
stopForeground(true);
你可能感兴趣的文章
985硕士:非科班自学编程感觉还不如培训班出来的,硕士白读了?
查看>>
你准备写代码到多少岁?程序员们是这么回答的!
查看>>
码农:和产品对一天需求,产品经理的需求是对完了,可我代码呢?
查看>>
程序员过年回家该怎么给亲戚朋友解释自己的职业?
查看>>
技术架构师的日常工作是什么?网友:搭框架,写公共方法?
查看>>
第四章 微信飞机大战
查看>>
九度:题目1008:最短路径问题
查看>>
九度Online Judge
查看>>
九度:题目1027:欧拉回路
查看>>
九度:题目1012:畅通工程
查看>>
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
51nod 分类
查看>>
1136 . 欧拉函数
查看>>
面试题:强制类型转换
查看>>
Decorator模式
查看>>
Template模式
查看>>
Observer模式
查看>>
高性能服务器设计
查看>>