Android12通知/小部件PendingIntents关闭后显示其他活动
创始人
2024-09-30 21:30:28
0

这是一个典型的"Android 12 (API 31)"的问题,新的行为限制了PendingIntent的使用,PendingIntent不能再启动应用程序的其他活动,必须是“目标”活动。这会导致某些从通知或小部件启动的活动意外退出或打开其他活动的问题。

要解决此问题,需要使用新的API,并添加以下操作:

1.在应用程序的 build.gradle 文件中添加以下依赖:

dependencies { implementation 'androidx.activity:activity-ktx:1.4.0-beta01' }

2.使用registerRemoteInputReceiver()方法注册RemoteInput接收器,以获取通知中的输入文本。

NotificationCompat.Action action = new NotificationCompat.Action.Builder( R.drawable.icon, "Reply", replyPendingIntent) .addRemoteInput(remoteInput) .build();

Notification notification = new NotificationCompat.Builder(context, channel_id) .setContentTitle("Title") .setContentText("Content") .setSmallIcon(R.drawable.icon) .addAction(action) .build();

// Step 1: Register the PendingIntent PendingIntent newReplyPendingIntent = PendingIntent.getBroadcast( context, 0, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

// Step 2: Build the RemoteInput RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY) .setLabel("Reply") .build();

// Step 3: Build the NotificationCompat.Action NotificationCompat.Action newReplyAction = new NotificationCompat.Action.Builder( R.drawable.icon, "Reply", newReplyPendingIntent) .addRemoteInput(remoteInput) .build();

// Step 4: Build the new NotificationCompat.Builder and set the actions NotificationCompat.Builder newBuilder = new NotificationCompat.Builder(context, channel_id) .setContentTitle("Title") .setContentText("Content") .setSmallIcon(R.drawable.icon) .addAction(newReplyAction);

// Step 5: Build the PendingIntent with NavDeepLinkBuilder // Ensure to set the NavDeepLinkBuilder with the correct activity and setIntent PendingIntent newContentIntent = new NavDeepLinkBuilder(context) .setGraph(R.navigation.nav_graph) // Your NavGraph .setDestination(R.id.destination_activity) // Your NavDestination .setIntent(intent) // Your Intent .createPendingIntent();

// Step 6: Set the content intent to the new PendingIntent newBuilder.setContentIntent(newContentIntent);

// Step 7: Build the new notification Notification newNotification = newBuilder.build();

相关内容

热门资讯

透视系统"德州局怎么... 这是一款非常优秀的德州局怎么透视 ia辅助检测软件,能够让你了解到德州局怎么透视中牌率当中全部隐藏参...
透视有挂"wepok... 透视有挂"wepoker透视脚本免费app"详细辅助技巧教程(私人定制透视技巧);大神普及一款德州a...
透视好牌"哈糖大菠萝... 透视好牌"哈糖大菠萝有挂吗5个常用方法"详细辅助黑科技教程(内置辅助教程);科技安装教程;13670...
透视插件"wepok... 透视插件"wepoker软件靠谱么"详细辅助大神讲解(软件技巧);支持2-10人实时对战,虚拟庄家发...
透视教学"竞技联盟透... 自定义竞技联盟透视插件系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管...
透视插件"hhpok... 透视插件"hhpoker智能辅助插件"详细辅助wpk教程(有辅助功能方法)是一款可以让一直输的玩家,...
透视好友房"wepo... 透视好友房"wepoker插件下载"详细辅助攻略教程(私人定制透视软件);大神普及一款德州ai内幕,...
透视神器"hh po... 透视神器"hh poker软件"详细辅助科技教程(辅助器安装包透视);hh poker软件软件透明挂...
透视免费"拱趴大菠萝... 透视免费"拱趴大菠萝万能辅助器"详细辅助AI教程(手机版透视脚本软件)关于拱趴大菠萝万能辅助器机制的...
透视透视挂"wepo... 1、透视透视挂"wepoker国外版透视"详细辅助2025新版总结(靠谱方法);详细教程。2、wep...