android 灰色保活使用,android app保活笔记

黑色保活

利用不同app进程使用的广播唤醒

开机、网络切换、拍照的系统广播

第三方SDK的广播

应用间的广播,BAT全家桶

灰色保活

API < 18,启动前台Service时直接传入new Notification()

API >= 18,同时启动两个id相同的前台Service,然后再将后启动的Service做stop处理

public class GrayService extends Service {

private final static int GRAY_SERVICE_ID = 1001;

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

if (Build.VERSION.SDK_INT < 18) {

startForeground(GRAY_SERVICE_ID, new Notification());//API < 18 ,此方法能有效隐藏Notification上的图标

} else {

Intent innerIntent = new Intent(this, GrayInnerService.class);

startService(innerIntent);

startForeground(GRAY_SERVICE_ID, new Notification());

}

return super.onStartCommand(intent, flags, startId);

}

...

...

/**

* 给 API >= 18 的平台上用的灰色保活手段

*/

public static class GrayInnerService extends Service {

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

startForeground(GRAY_SERVICE_ID, new Notification());

stopForeground(true);

stopSelf();

return super.onStartCommand(intent, flags, startId);

}

}

}

查看是否有app使用这种方式,在adb shell下dumpsys activity services PackageName,是否有isForeground=true但是通知栏没有通知的情况

白色保活

启动一个前台service,会在系统通知栏中显示

AccountManager

oom_adj

什么是oom_adj?它是linux内核分配给每个系统进程的一个值,代表进程的优先级,进程回收机制就是根据这个优先级来决定是否进行回收。对于oom_adj的作用,你只需要记住以下几点即可:

进程的oom_adj越大,表示此进程优先级越低,越容易被杀回收;越小,表示进程优先级越高,越不容易被杀回收

普通app进程的oom_adj>=0,系统进程的oom_adj才可能<0

查看oom_adj

cat /proc/进程ID/oom_adj

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值