forceStop流程会把对应进程的pendingIntent给cancel掉

首先对比Android U和Android V的代码在forceStop时候的处理

Android U

复制代码
 @GuardedBy("this")
    final boolean forceStopPackageLocked(String packageName, int appId,
            boolean callerWillRestart, boolean purgeCache, boolean doit,
            boolean evenPersistent, boolean uninstalling, int userId, String reasonString,
            int reason) {
      ...

        if (packageName == null || uninstalling) {
            didSomething |= mPendingIntentController.removePendingIntentsForPackage(
                    packageName, userId, appId, doit);
        }

    ....

        return didSomething;
    }

Android V

java 复制代码
@GuardedBy("this")
final boolean forceStopPackageLocked(String packageName, int appId,
        boolean callerWillRestart, boolean purgeCache, boolean doit,
        boolean evenPersistent, boolean uninstalling, boolean packageStateStopped,
        int userId, String reasonString, int reason) {
    ....

    boolean clearPendingIntentsForStoppedApp = false;
    try {
        // stop情况下,clearPendingIntentsForStoppedApp 为true
        clearPendingIntentsForStoppedApp = (packageStateStopped
                && android.content.pm.Flags.stayStopped());
    } catch (IllegalStateException e) {
        // It's unlikely for a package to be force-stopped early in the boot cycle. So, if we
        // check for 'packageStateStopped' which should evaluate to 'false', then this should
        // ensure we are not accessing the flag early in the boot cycle. As an additional
        // safety measure, catch the exception and ignore to avoid causing a device restart.
        clearPendingIntentsForStoppedApp = false;
    }
    if (packageName == null || uninstalling || clearPendingIntentsForStoppedApp) {
        final int cancelReason;
        if (packageName == null) {
            cancelReason = PendingIntentRecord.CANCEL_REASON_USER_STOPPED;
        } else if (uninstalling) {
            cancelReason = PendingIntentRecord.CANCEL_REASON_OWNER_UNINSTALLED;
        } else {
            cancelReason = PendingIntentRecord.CANCEL_REASON_OWNER_FORCE_STOPPED;
        }
        didSomething |= mPendingIntentController.removePendingIntentsForPackage(
                packageName, userId, appId, doit, cancelReason);
    }
    ....

    return didSomething;
}

可以看到Androidv相比于Androidu,多了一个 clearPendingIntentsForStoppedApp,即在应用被forceStop的时候,也会移除对应的pendingIntent

对应的堆栈调用链

并且会移除alarmManager中的PendingIntent

java 复制代码
    private void makeIntentSenderCanceled(PendingIntentRecord rec,
            @CancellationReason int cancelReason) {
        rec.canceled = true;
        rec.cancelReason = cancelReason;
        final RemoteCallbackList<IResultReceiver> callbacks = rec.detachCancelListenersLocked();
        if (callbacks != null) {
            final Message m = PooledLambda.obtainMessage(
                    PendingIntentController::handlePendingIntentCancelled, this, callbacks);
            mH.sendMessage(m);
        }
        final AlarmManagerInternal ami = LocalServices.getService(AlarmManagerInternal.class);
        ami.remove(new PendingIntent(rec));
    }

根据PendingIntent的使用场景,可能会出现以下问题:

1.脑钟应用进程再被forceStop之后,如果用户不再去打开闹钟,那么闹钟到点不会响铃

2.音乐类应用传入meidaButton中的pendingIntent失效,导致应用进程被杀之后,无法通过耳机拉起音乐进程

相关推荐
消失的旧时光-19434 小时前
Flutter 响应式 + Clean Architecture / MVU 模式 实战指南
android·flutter·架构
404未精通的狗4 小时前
(数据结构)栈和队列
android·数据结构
恋猫de小郭5 小时前
今年各大厂都在跟进的智能眼镜是什么?为什么它突然就成为热点之一?它是否是机会?
android·前端·人工智能
游戏开发爱好者87 小时前
iOS 混淆工具链实战 多工具组合完成 IPA 混淆与加固 无源码混淆
android·ios·小程序·https·uni-app·iphone·webview
豆豆豆大王12 小时前
Android 数据持久化(SharedPreferences)
android
Paper_Love12 小时前
RK3588-android-reboot命令内核调用流程
android
介一安全12 小时前
【Frida Android】基础篇12:Native层hook基础——调用原生函数
android·网络安全·逆向·安全性测试·frida·1024程序员节
2501_9160088913 小时前
用多工具组合把 iOS 混淆做成可复用的工程能力(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)
android·开发语言·ios·小程序·uni-app·iphone·swift
Zach_yuan13 小时前
程序地址空间
android·linux·运维·服务器
带电的小王13 小时前
llama.cpp:Android端测试Qwen2.5-Omni
android·llama.cpp·qwen2.5-omni