android notification点击弹出对话框
我用自定义类继承broadcastreceiver接收系统的广播,然后在这个类(Receiver)中添加了一个notification,现在我想点击这个通知,弹出一个对话...
我用自定义类继承broadcast receiver接收系统的广播,然后在这个类(Receiver)中添加了一个notification,现在我想点击这个通知,弹出一个对话框,这个该怎样实现?
展开
2个回答
展开全部
我想你要的功能应该是图中的这个吧?在API demo中有示例的。需要用到IncomingMessageInterstitial
点击触发通知:
void showInterstitialNotification() {
// look up the notification manager service
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// The details of our fake message
CharSequence from = "Dianne";
CharSequence message;
switch ((new Random().nextInt()) % 3) {
case 0: message = "i am ready for some dinner"; break;
case 1: message = "how about thai down the block?"; break;
default: message = "meet u soon. dont b late!"; break;
}
// The PendingIntent to launch our activity if the user selects this
// notification. Note the use of FLAG_CANCEL_CURRENT so that, if there
// is already an active matching pending intent, cancel it and replace
// it with the new Intent.
Intent intent = new Intent(this, IncomingMessageInterstitial.class);
intent.putExtra(IncomingMessageView.KEY_FROM, from);
intent.putExtra(IncomingMessageView.KEY_MESSAGE, message);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
// The ticker text, this uses a formatted string so our message could be localized
String tickerText = getString(R.string.imcoming_message_ticker_text, message);
// construct the Notification object.
Notification notif = new Notification(R.drawable.stat_sample, tickerText,
System.currentTimeMillis());
// Set the info for the views that show in the notification panel.
notif.setLatestEventInfo(this, from, message, contentIntent);
// We'll have this notification do the default sound, vibration, and led.
// Note that if you want any of these behaviors, you should always have
// a preference for the user to turn them off.
notif.defaults = Notification.DEFAULT_ALL;
// Note that we use R.layout.incoming_message_panel as the ID for
// the notification. It could be any integer you want, but we use
// the convention of using a resource id for a string related to
// the notification. It will always be a unique number within your
// application.
nm.notify(R.string.imcoming_message_ticker_text, notif);
}
创建一个Activity来接收通知:
public class IncomingMessageInterstitial extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.incoming_message_interstitial);
Button button = (Button) findViewById(R.id.notify_app);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
switchToApp();
}
});
}
/**
* Perform a switch to the app. A new activity stack is started, replacing
* whatever is currently running, and this activity is finished.
*/
void switchToApp() {
// We will launch the app showing what the user picked. In this simple
// example, it is just what the notification gave us.
CharSequence from = getIntent().getCharSequenceExtra(IncomingMessageView.KEY_FROM);
CharSequence msg = getIntent().getCharSequenceExtra(IncomingMessageView.KEY_MESSAGE);
// Build the new activity stack, launch it, and finish this UI.
Intent[] stack = IncomingMessage.makeMessageIntentStack(this, from, msg);
startActivities(stack);
finish();
}
}
具体可以参考API demos里的IncomingMessageInterstitial.java和IncomingMessage.java
追问
第一部分是定义在一个activity里面么?第16行里面Intent的第一个参数this,是什么?如果在broadcast receiver里面定义showInterstitialNotification() ,this该填什么?
追答
是的,这个代码是放在Activity里,但是你放到broadcastreceiver里也没关系,this是context
展开全部
弹出对话框没做过,但弹出Activity会做吧,在Activity弹出对话框也很好做,让后把整个Activity设置成透明的,这样看起来跟只弹出一个对话框的效果是一样的。
追问
弹出activity也弄不来。。。 主要是在broadcast receiver中定义这个功能,弹出activity要有intent吧,这个intent的上下文cntext该用哪个? 用onReceive的第一个参数会在接收到消息的时候崩溃。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询