1. 现象
Killing process:pid: 4967,(com.android.lava.powersave),adj=900, mem=17193KB
06-11 10:09:18.064335 4967 4967 I LavaPowerSaveService: LavaPowerSaveServicestart
06-11 10:10:14.558997 6120 6120 I LavaPowerSaveService: LavaPowerSaveServicestart
06-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 以后
对应如下图
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);