优化第三方应用被kill后接收广播自启

本文探讨了如何在Android系统中优化广播接收器的行为,特别是针对第三方应用。当接收到特定系统广播,如网络连接变化、设备启动等,系统默认会尝试重启应用。通过设置系统属性`persist.sys.disable_keepalive`,可以避免非系统应用因这些广播而被唤醒,从而提高系统效率和电池寿命。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--- a/services/core/java/com/android/server/am/BroadcastQueue.java
+++ b/services/core/java/com/android/server/am/BroadcastQueue.java
@@ -921,6 +924,33 @@ public final class BroadcastQueue {
// restart the application.
}
+ // app not started, do not start if thirdpart app at these intents.
+ boolean disableKeepAlive = SystemProperties.getBoolean("persist.sys.disable_keepalive", true);
+ if (disableKeepAlive) {
+ String action = r.intent.getAction();
+ ApplicationInfo appInfo = info.activityInfo.applicationInfo;
+ Slog.d(TAG, "action "+action+" app:"+appInfo);
+ if (appInfo != null && (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
+ Slog.d(TAG, "Not system app!!! [" + action + "] app=" + app );
+
+ if (action.equals("android.net.conn.CONNECTIVITY_CHANGE") ||
+ action.equals("android.intent.action.BOOT_COMPLETED") ||
+ action.equals("android.intent.action.MEDIA_MOUNTED") ||
+ action.equals("android.intent.action.MEDIA_REMOVED") ||
+ action.equals("android.intent.action.TIME_TICK") ||
+ action.equals("android.intent.action.MEDIA_UNMOUNTED") ||
+ action.equals("android.intent.action.MEDIA_BAD_REMOVAL") ||
+ action.equals("android.intent.action.MEDIA_SHARED")
+ ) {
+ logBroadcastReceiverDiscardLocked(r);
+ finishReceiverLocked(r, r.resultCode, r.resultData,
+ r.resultExtras, r.resultAbort, false);
+ scheduleBroadcastsLocked();
+ r.state = BroadcastRecord.IDLE;
+ return;
+ }
+ }
+ }
// Not running -- get it started, to be executed when the app comes up.
if (DEBUG_BROADCAST) Slog.v(TAG,
"Need to start app ["



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值