如何在用户点击时取消通知?

时间:2011-10-07 00:41:00

标签: android notifications

我想创建一个通知,当用户按下该通知并且没有其他行为时会自动清除该通知。下面的通知工作正常,但是当我按下它时,它会将我带到活动“MyActivity”(当我不想使用它时,甚至必须定义一个意图似乎有点不必要...)

使用FLAG_AUTO_CANCEL似乎没有任何效果。

更新: 抱歉,我发现FLAG_AUTO CANCEL确实有效,即通知已从状态栏中清除。我想我真的想写一个什么都不做的意图(或完全删除意图)。

代码......

             Notification notification = new Notification(R.drawable.success, res.getString(R.string.messages_sent), System.currentTimeMillis());

            //Define the expanded message and intent
            Context context = getApplicationContext();
            CharSequence contentTitle = res.getString("My content title");


            Intent notificationIntent = new Intent(this, MyActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0 );
            notification.setLatestEventInfo(context, contentTitle, mStrSuccessMessage, contentIntent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            //Pass the notification to the notification manager
            mNotificationManager.notify(1, notification);

2 个答案:

答案 0 :(得分:0)

我认为你不能。您可以立即退出MyActivity:从finish()致电onCreate()

答案 1 :(得分:0)

在没有实际操作的情况下使用空意图。

PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(), 0);
相关问题