首页 > 解决方案 > 来自 IntentService 的启动活动在 Android 12 中不起作用

问题描述

快速背景:我已将我的最新更新targetSdkVersion到最新的 Android 31

我有一个在从通知托盘单击通知后执行的服务类,它包含以下代码:

public class NotificationClickListener extends IntentService {

    @Override
    protected void onHandleIntent(Intent intent) {
        Intent myIntentFromPendingIntent = intent;
        //insert some necessary code

        myIntentFromPendingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(myIntentFromPendingIntent); //this line here gets executed but the application or the activity wasn't opened after this line
    }

收到消息后,将创建 PendingIntent,如下所示

    PendingIntent contentIntent = PendingIntent.getService(appContext, 0, intent, PendingIntent.FLAG_IMMUTABLE);
    mNotificationManager.notify(0, myNotification);

我没有收到任何错误日志或任何相关的跟踪日志,我看到的唯一日志是

    D/skia: --- Failed to create image decoder with message 'unimplemented'

我浏览了一些文档并偶然发现了Restrictions on started Activity from the background,并且列出了一些例外情况,其中之一是当“应用程序在前台任务的后台堆栈中有活动”时。所以我期待我的活动​​会开始(鉴于我的应用程序在后台暂停)


谁能帮我定位问题?是否仍然可以使用 IntentService 解决此问题?

或者现在是否需要使用 WorkManager 进行迁移?如果是这样,你也可以留下一个很好的参考来检查。

非常感谢。

标签: androidpush-notificationintentserviceandroid-workmanagerandroid-12

解决方案


所以我期待我的活动​​会开始(鉴于我的应用程序在后台暂停)

FWIW 似乎它不符合您引用的规则:“该应用程序在前台任务的后台堆栈中有一个活动。” (强调)——你的应用程序在后台,而不是前台。

我有一个在从通知托盘单击通知后执行的服务类,它包含以下代码:

让您的通知直接启动活动。一旦您targetSdkVersion达到 31 岁或更高,就禁止使用此类“蹦床” 。


推荐阅读