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();

相关内容

热门资讯

透视安装!wepoker免费脚... 透视安装!wepoker免费脚本弱密码(透视)钱塘十三水其实真的有挂(果然存在有辅助安装)-哔哩哔哩...
透视规律!poker worl... 透视规律!poker world辅助(透视)蛮王大厅辅助教程(果然是有辅助app)-哔哩哔哩1、玩家...
今天下午!aapoker免费透... 今天下午!aapoker免费透视脚本(透视)微乐斗地主有挂吗(好像存在有辅助插件)-哔哩哔哩1、用户...
透视肯定!约局吧游戏挂(透视)... 透视肯定!约局吧游戏挂(透视)圈麻圈脚本辅助(总是真的有辅助神器)-哔哩哔哩1、完成圈麻圈脚本辅助辅...
此事迅速冲上热搜!德州机器人代... 此事迅速冲上热搜!德州机器人代打脚本(透视)遇悦游戏辅助(确实有辅助修改器)-哔哩哔哩1、每一步都需...
透视实锤!德州透视是真的假的(... 透视实锤!德州透视是真的假的(透视)亿游十三道辅助(果然存在有辅助下载)-哔哩哔哩;该软件可以轻松地...
随着!wepoker私人局规律... 随着!wepoker私人局规律(透视)蜀山四川小程序作必弊码大全(原来是真的辅助修改器)-哔哩哔哩1...
截至目前!wpk是真的还是假的... 截至目前!wpk是真的还是假的(透视)潮汕汇木虱辅助下载(竟然是有辅助软件)-哔哩哔哩1、潮汕汇木虱...
在玩家背景下!wepoker黑... 在玩家背景下!wepoker黑侠辅助器正版下载(透视)开心泉州辅助(其实是有辅助app)-哔哩哔哩1...
透视中牌率!哈糖大菠萝怎么开挂... 透视中牌率!哈糖大菠萝怎么开挂(透视)打两圈辅助软件(好像真的有辅助下载)-哔哩哔哩1、哈糖大菠萝怎...