gpt4 book ai didi

android - Pending Intent 有生命周期吗?

转载 作者:行者123 更新时间:2023-12-04 17:24:53 26 4
gpt4 key购买 nike

我有一个运行前台服务的应用程序。该应用程序有一个启动/停止按钮作为其通知的一部分,可以猜到启动和停止前台服务。单击开始按钮后,将触发一个待处理的 Intent。

考虑以下场景:

应用程序已被销毁[从最近的项目列表中删除]但通知仍然可见。即使应用程序已被销毁,我也能够启动和停止前台服务,因为单击通知按钮会触发 Pending Intent (这又会调用广播接收器)。但是,我观察到的是,一两天后,单击通知上的按钮后,未触发未决 Intent (即前台服务未启动)。这就引出了一个问题,在包含的应用程序被销毁后,挂起的 Intent 是否有生命周期?还是我在这里遗漏了其他东西?

我的未决意向调用:

Intent notificationBroadcastIntent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent playIntent = PendingIntent.getBroadcast(this, MY_REQUEST_CODE,
notificationBroadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

由未决 Intent 调用的广播接收器(依次启动前台服务):

@Override
public void onReceive(Context context, Intent intent) {
Log.i(MyBroadcastReceiver.class.getSimpleName(), "MyBroadcastReceiver called to update service notification");

if (isMyForegroundServiceRunning(MyForegroundService.class, context)) {
Intent stopIntent = new Intent(context, MyForegroundService.class);
stopIntent.setAction(STOP_SERVICE_ACTION);
context.startService(stopIntent);
} else {
Intent startIntent = new Intent(context, MyForegroundService.class);
startIntent.setAction(START_SERVICE_ACTION);
context.startService(startIntent);
}
}

最佳答案

PendingIntent 不会过期。但是,它们不是持久性的,不会在设备重启后继续存在。

要查看 PendingIntent 是否仍然存在,您可以使用以下 adb 命令:

adb shell dumpsys Activity Intent

这列出了系统中所有的 PendingIntent。您还可以使用以下方式查看通知:

adb shell dumpsys 通知

这将显示所有 Notification,包括为它们设置的 PendingIntent

关于android - Pending Intent 有生命周期吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64123524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com