【问题标题】:how to use delete intent to perform some action on clear notification?如何使用删除意图对清除通知执行一些操作?
【发布时间】:2012-10-23 10:09:49
【问题描述】:

当用户清除我的通知时,我想重置我的服务变量:仅此而已!

环顾四周,我看到每个人都建议在我的通知上添加删除意图,但意图用于启动活动、服务或任何东西,而我只需要这样的东西:

void onClearPressed(){
   aVariable = 0;
}

如何得到这个结果?

【问题讨论】:

    标签: android


    【解决方案1】:

    通知不是由您的应用程序管理的,所有诸如显示通知和清除通知之类的事情实际上都是在另一个进程中发生的。出于安全原因,您不能让另一个应用程序直接执行一段代码。

    在您的情况下,唯一的可能性是提供一个 PendingIntent,它只包含一个常规 Intent,并在清除通知时代表您的应用程序启动。 您需要使用PendingIntent 发送广播或启动服务,然后在广播接收器或服务中执行您想要的操作。具体使用什么取决于您显示通知的应用程序组件。

    如果是广播接收器,您可以为广播接收器创建一个匿名内部类,并在显示通知之前动态注册它。它看起来像这样:

    public class NotificationHelper {
        private static final String NOTIFICATION_DELETED_ACTION = "NOTIFICATION_DELETED";
    
        private final BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                aVariable = 0; // Do what you want here
                unregisterReceiver(this);
            }
        };
    
        public void showNotification(Context ctx, String text) {
            Intent intent = new Intent(NOTIFICATION_DELETED_ACTION);
            PendingIntent pendintIntent = PendingIntent.getBroadcast(ctx, 0, intent, 0);
            registerReceiver(receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
            Notification n = new Notification.Builder(mContext).
              setContentText(text).
              setDeleteIntent(pendintIntent).
              build();
            NotificationManager.notify(0, n);
        }
    }
    

    【讨论】:

    • 我正在尝试使用 intentService。获取异常意图接收器被泄露。 “您是否错过了取消注册接收器的呼叫?”任何人都可以对此有所了解吗?
    • 注册和注销 Receiver 应该是从一个未引用的上下文中调用的,所以在你的 intentService 中使用 getApplicationContext() 应该可以解决你的问题。
    • 我建议注册静态接收器(例如在 AndroidManifest 中注册),因为应用程序可能会在用户关闭通知时被终止。静态接收器确保即使应用程序已经死了,代码也会被执行。
    • 只有当广播接收器是公共类或静态嵌套类时,才可以静态注册广播接收器。因此,您没有要更改的变量的上下文
    【解决方案2】:

    安德烈是正确的。
    如果您想要返回多条消息,例如:

    • 您想知道消息是否被点击
    • 你附加了一个带有你想捕捉的图标的动作
    • 并且您想知道消息是否被取消

    您必须注册每个响应过滤器:

    public void showNotification(Context ctx, String text) ()
    {
        /… create intents and pending intents same format as Andrie did../
        /… you could also set up the style of your message box etc. …/
    
        //need to register each response filter
        registerReceiver(receiver, new IntentFilter(CLICK_ACTION));
        registerReceiver(receiver, new IntentFilter(USER_RESPONSE_ACTION));
        registerReceiver(receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
    
        Notification n = new Notification.Builder(mContext)
          .setContentText(text)
          .setContentIntent(pendingIntent)                          //Click action
          .setDeleteIntent(pendingCancelIntent)                     //Cancel/Deleted action
          .addAction(R.drawable.icon, "Title", pendingActionIntent) //Response action
          .build();
    
        NotificationManager.notify(0, n);
    
    }
    

    然后您可以使用 if、else 语句(如 Andrei 所做的)或 switch 语句来捕捉不同的响应。

    注意:我做出这个回应主要是因为我在任何地方都找不到这个,我必须自己弄清楚。 (也许我会更好地记住它 :-) 玩得开心!

    【讨论】:

    • 哦,你也应该考虑查找 NotificationCompat.Builder
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多