【问题标题】:Android: Notification action to dismiss the notificationAndroid:关闭通知的通知操作
【发布时间】:2013-06-01 06:23:23
【问题描述】:

我正在尝试使用操作按钮创建一个 android 通知。我已经设置了一个启动活动,但我想要另一个关闭通知并且不将用户带到另一个屏幕/活动(未带到我的应用程序的任何部分)

我不想使用 '.auto cancel(true) ',因为我希望保留通知,除非按下此操作按钮。

主要用途是持续通知,因为它们无法通过滑动删除。这个想法类似于带有“解雇”动作的android日历。

【问题讨论】:

  • 你能详细说明一下吗?您的目标是什么 API 级别?您应该能够通过 jellybean+ 通知来完成所有这些操作。
  • 我正在使用 jellybean api - 我已经做了一个开始活动的动作,购买我不知道如何不开始活动。我可以使用广播接收器,但我不知道这是否能让我清除通知。基本上我想要一个通知动作来清除自己(通知)

标签: android notifications action


【解决方案1】:

创建通知后,将通知发送到广播接收器。广播接收器是在按下通知操作时启动的,因此广播接收器中的 cancel() 会取消通知。

【讨论】:

  • 您能否给出确切的代码,我无法按照您的指示进行操作。谢谢。
【解决方案2】:

IMO 使用广播接收器是取消通知的一种更简洁的方法:

在 AndroidManifest.xml 中:

<receiver
     android:name=.NotificationCancelReceiver" >
     <intent-filter android:priority="999" >
         <action android:name="com.example.cancel" />
     </intent-filter>
</receiver>

在java文件中

Intent cancel = new Intent("com.example.cancel");
PendingIntent cancelP = PendingIntent.getBroadcast(context, 0, cancel, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Action actions[] = new NotificationCompat.Action[1];

NotificationCancelReceiver:

public class NotificationCancelReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
         //Cancel your ongoing Notification
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    相关资源
    最近更新 更多