Android 如何将活动从通知启动到前台?
目标:我的目标是在用户点击通知时在前台启动Android活动。 如果打开了,点击通知将关闭通知抽屉,活动将按预期在前台打开 问题:如果没有打开应用程序的任何任务,但点击通知后,通知抽屉仍保留在前台,活动仅在后台打开 问题:如何告诉Android关闭通知抽屉并将打开的活动放在前台,无论应用程序是否打开了任务 代码片段:Android 如何将活动从通知启动到前台?,android,android-activity,android-notifications,foreground,Android,Android Activity,Android Notifications,Foreground,目标:我的目标是在用户点击通知时在前台启动Android活动。 如果打开了,点击通知将关闭通知抽屉,活动将按预期在前台打开 问题:如果没有打开应用程序的任何任务,但点击通知后,通知抽屉仍保留在前台,活动仅在后台打开 问题:如何告诉Android关闭通知抽屉并将打开的活动放在前台,无论应用程序是否打开了任务 代码片段: val intent = Intent(context, SplashActivity::class.java) intent.addFlags( Intent.FLAG_
val intent = Intent(context, SplashActivity::class.java)
intent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP
)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
NotificationCompat.Builder(context, channelId)
.build(title, text, icon, pendingIntent)
您可以使用
pendingent
来完成此操作
看看怎么样。希望这能有所帮助。如果我是对的,您希望在活动打开时关闭通知抽屉。 因此,当您从通知打开活动时,请在代码中添加这两行
Intent x = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(x);