该方法应该是
Android系统前台service的一个漏洞,使用该方法启动服务后,
通过adb shell cat proc/进程号/oom_adj 查看,运行在后台的服务其进程号(7.0后台进程为12)变成了
2(6.0是2,7.0是4,
前台进程0
)
步骤:
1、常驻服务内启动时作为前台服务启动,并启动一个只用来辅助提高进程优先级的服务
FakeService
:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(FakeService.NOTIFY_ID, new Notification());
startService(new Intent(this, FakeService.class));
return super.onStartCommand(intent, flags, startId);
}
2、辅助服务FakeService也启动为前台服务,但是一启动马上stop掉,onDestroy中调用stopForeground()方法,这样可以把常驻服务设置为前台服务了,而通知栏也不会有通知显示。
public static int NOTIFY_ID = 100101;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Utils.logI(TAG, "onStartCommand");
startForeground(NOTIFY_ID, new Notification());
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
stopForeground(true);
super.onDestroy();
}
版权声明:本文为forLittleBlue原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。