android - 从 IntentService 启动 Activity 在 Android 12 中不起作用

标签 android push-notification intentservice android-workmanager 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 starting activities from the background ,并且有一些列出的异常,其中之一是当“该应用程序在前台任务的后台堆栈中有一个 Activity ”时。所以我期待我的 Activity ​​会开始(假设我的应用程序在后台暂停)


谁能帮我找出问题所在?是否仍然可以使用 IntentService 解决此问题?

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

非常感谢。

最佳答案

So I was expecting that my activity would be started (given that my application was paused in the background)

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

I have a service class that gets executed after a notification is clicked from the notification tray, it contains below code:

让您的通知直接启动 Activity 。像这样的“蹦床”are banned once your targetSdkVersion reaches 31 or higher .

关于android - 从 IntentService 启动 Activity 在 Android 12 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69222403/

相关文章:

Android如何检查 Intent 服务是否仍在运行或已停止运行

android - 两个或多个类可以向同一个 IntentService 发送数据吗?

值-b+sr+Latn 的 Android 构建错误

android - 哪款 Android 手机用于开发...?

java - simple_list_item_2 模式下的空 ListView

android - 为什么 android studio 在应用文件模板后进行 gradle 同步?

android - 是否可以仅使用 WonderPush REST API 向 Android 设备发送推送通知而不使用其 SDK?

android - 手动结束 IntentService 工作线程

android - FCM 推送通知 Android 在后台接收 2 个通知

websphere - Worklight - 使用 URL 从非 Worklight 应用程序调用推送通知适配器