点击通知后关闭状态栏

6

我的通知包含几个按钮:

  • 1个按钮启动主活动(这样做时应关闭状态栏)
  • 其中4个按钮会发送挂起意图来控制音乐(应保持状态栏打开)

问题是,第一个按钮没有关闭状态栏...

第一个按钮发送的PendingIntent:

Intent activityIntent = new Intent(smp, MusicShaker.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
activityIntent.setAction(MyIntentAction.DO_LAUNCH_ACTIVITY_FROM_NOTIF);
remoteViews.setOnClickPendingIntent(R.id.notif_album_IV, PendingIntent
            .getActivity(ctxt, 0, activityIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT));

活动正确启动,但状态栏仍然存在,没有自动关闭。
我是否遗漏或误解了某个标志?我能否在MyActivity.onResume()中编程关闭状态栏?
编辑:顺便说一下,通知是由一个服务推送的。
谢谢 =)

可能是点击Android通知操作不关闭通知抽屉的重复问题。 - Michael Litvin
4个回答

4
你只需要在创建构建器时指定 setAutoCancel(true)。就这么简单 :)
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("title goes here")
            .setContentText("content text goes here").setAutoCancel(true);

2

好的,我找到了解决方案... 我无法复制标准通知产生的相同行为,所以我 :
- 将我的imageButton“notif_album_IV”设置为不可点击,并将其更改为ImageView
- 使用了以下代码:

builder.setContentIntent(PendingIntent.getActivity(smp, 0,
  activityIntent,PendingIntent.FLAG_CANCEL_CURRENT))

不需要手动设置setImagePendingIntent,而是通过内容背景处理意图广播


2
这对我有用:

这对我有用:

sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

自 Android 12 起,此功能已被弃用。 - Jerin

1

是的,当您的应用程序启动时,您将需要以编程方式取消通知。

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

这个解决方案允许我在不关闭状态栏的情况下移除通知,而我实际上需要相反的行为(关闭状态栏而不移除通知)。 - elgui

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接