You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 11及以上版本AlarmManager保活非前台服务方案是否仍有效?

基于AlarmManager的保活方案在Android 11及以上版本的运行情况
  • 原有代码首先存在适配问题:Android 12(API 31)及以上版本要求PendingIntent必须显式指定可变或不可变属性,你原有代码中使用的PendingIntent.FLAG_CANCEL_CURRENT没有补充对应标记,在API 31及以上运行会直接抛出异常。如果要兼容高版本,需要修改PendingIntent的声明逻辑:
void startServicesAliveChecker(){
    Intent postponeIntent = new Intent(this,AlarmReceiver.class);
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Calendar c = Calendar.getInstance();
    long t = c.getTimeInMillis()+3000;
    PendingIntent pendingIntent;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        pendingIntent = PendingIntent.getBroadcast(this, 333,postponeIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    } else {
        pendingIntent = PendingIntent.getBroadcast(this, 333,postponeIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    }
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP,t,15000,pendingIntent);
}
  • 触发间隔限制:Android 12开始,后台应用使用setInexactRepeating设置的重复间隔如果小于15分钟,系统会自动将间隔拉长至最少15分钟,你设置的15秒触发间隔不会生效。
  • 进程被杀死后的触发限制:如果应用被用户从最近任务列表划掉,或者被系统低内存机制查杀,原生Android 11及以上会保留Alarm任务,但国内主流厂商的定制ROM(小米、华为、OPPO、vivo等)默认会直接清除对应应用的所有Alarm注册,除非用户手动给应用开启「自启动权限」「后台活动权限」,否则Alarm不会触发。
  • 后续服务启动限制:就算Alarm成功触发AlarmReceiver,Android 12及以上禁止后台应用在无用户交互的情况下启动前台服务,普通后台服务启动后最多存活数分钟就会被系统强制回收,无法实现长期维持非前台服务运行的效果。
  • 政策合规问题:这类非常规保活逻辑违反Google Play的应用上架政策,上架审核时会被排查到,直接做下架处理。

内容的提问来源于stack exchange,提问作者Anonymous

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 22:18:03