繁体   English   中英

用户单击通知时如何取消通知?

[英]How can I cancel a notification when the user clicks on it?

我想创建一个通知,该通知在用户按下时会自动清除,并且没有其他行为。 下面的通知工作正常,但是当我按它时,它会将我带到活动“ 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);

我认为你不能。 不过,您可以立即退出MyActivity :从onCreate()调用finish() onCreate()

在没有实际行动的情况下使用空意图。

PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(), 0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM