Android 通知点击事件时调用活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3666250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call activity when on notification tap event
提问by Kandha
I want to call the activity when user pull down the notification and tap on that notification. How can I do that?
我想在用户下拉通知并点击该通知时调用该活动。我怎样才能做到这一点?
回答by CommonsWare
Call setLatestEventInfo()
on the Notification
object, supplying a PendingIntent
that will start your activity when they tap on your entry in the notification drawer. Here is a sample projectdemonstrating this.
调用setLatestEventInfo()
该Notification
对象,提供一个PendingIntent
当他们点击通知抽屉中的条目时将启动您的活动。这是一个示例项目,演示了这一点。
回答by DonGru
Assuming that notif
is your Notification
object:
假设这notif
是你的Notification
对象:
Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
notif.contentIntent = contentIntent;
回答by Harsha.Vaswani
Here is the code to call activity when notification is clicked
这是单击通知时调用活动的代码
Notification notif = new Notification(R.drawable.ic_launcher,"List of Contacts...", System.currentTimeMillis());
Intent notificationIntent = new Intent(context,AllContacts.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);