android - 来自 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 进行迁移?如果是这样,你也可以留下一个很好的参考来检查。
非常感谢。
解决方案
所以我期待我的活动会开始(鉴于我的应用程序在后台暂停)
FWIW 似乎它不符合您引用的规则:“该应用程序在前台任务的后台堆栈中有一个活动。” (强调)——你的应用程序在后台,而不是前台。
我有一个在从通知托盘单击通知后执行的服务类,它包含以下代码:
让您的通知直接启动活动。一旦您targetSdkVersion
达到 31 岁或更高,就禁止使用此类“蹦床” 。
推荐阅读
- python - 如果 if 语句在函数之外,是否可以将 else 放入函数中?
- python - python从串行传感器实时绘图
- c++ - 使用 system() 函数在 C++ 程序中运行 cmd 命令
- ios - Xcode 10 - iOS 12 Simulator 位置模拟在 City Bicycle Ride/City Run/Freeway Drive 上失败
- c# - 带有 MVC 区域的 Umbraco - System.InvalidCastException:从 'System.String' 到 'System.Nullable'1 [[System.Guid 的无效转换
- excel - Excel VBA行中的第一个非空单元格
- excel - Excel VBA:初始化用户表单时搜索特定字符串的工作表
- groovy - 在 VSCode 中编译 Groovy
- apache-spark - 为什么 spark 中的累加器对工作任务是只写的?
- mysql - 多连接中的行总和