【问题标题】:Close Status Bar after Notification click单击通知后关闭状态栏
【发布时间】:2012-08-21 15:55:59
【问题描述】:

我的通知包含几个按钮:

  • 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 notifications statusbar


【解决方案1】:

您只需要在创建构建器时指定 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】:

    是的,您必须在应用启动时以编程方式取消通知。

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

    【讨论】:

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

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

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

    不是在图像上“手动”设置 setOnClickPendingIntent,而是由内容背景处理意图广播

    【讨论】:

      【解决方案4】:

      这对我有用:

      sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
      

      【讨论】:

      • 自 android 12 以来已弃用
      猜你喜欢
      • 2013-03-12
      • 2013-10-26
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多