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失效,导致应用进程被杀之后,无法通过耳机拉起音乐进程

相关推荐
雨白19 小时前
Jetpack Compose Navigation 2.x 详解
android·android jetpack
Android系统攻城狮21 小时前
Android内核进阶之获取DMA地址snd_pcm_sgbuf_get_addr:用法实例(九十一)
android·pcm·android内核·音频进阶·pcm硬件参数
清空mega1 天前
Android Studio移动应用基础教程(前言)
android·ide·android studio
2501_937145411 天前
2025IPTV 源码优化版实测:双架构兼容 + 可视化运维
android·源码·源代码管理·机顶盒
zhoutanooi1 天前
安卓bp文件编译学习
android·学习
aramae1 天前
MySQL数据库入门指南
android·数据库·经验分享·笔记·mysql
百锦再1 天前
选择Rust的理由:从内存管理到抛弃抽象
android·java·开发语言·后端·python·rust·go
whatever who cares1 天前
在Java/Android中,List的属性和方法
android·java
油炸小波1 天前
09-微服务原理篇(XXLJOB-幂等-MySQL)
android·mysql·微服务
果子没有六分钟1 天前
setprop debug.hwui.profile visual_bars有什么作用
android