2

我遇到了一些问题。我有一个AlarmManager向BroadcastReceiver发送意图,在该类的onReceive方法中,我将通知推送到StatusBar ...这很像一个魅力,但我需要在用户点击通知时打开一个活动,但在谷歌上搜索,并在这样我无法找到答案后寻找我的代码的一些问题......所以这是我的代码推通知符:打开BroadcastReceiver发送的通知时打开活动

Intent intent = new Intent(Intent.ACTION_MAIN); 
intent.setClass(this.context, NewCommit.class);  
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

PendingIntent sender = PendingIntent.getBroadcast(this.context, 192839, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 

NotificationCompat.Builder noti = new NotificationCompat.Builder(this.context) 
    .setSmallIcon(R.drawable.status_bar_icon) 
    .setContentTitle("My notification") 
    .setContentText("Hello World!") 
    .setContentIntent(sender); 



noti.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); 


NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(181818, noti.build()); 

任何想法?这让我疯狂!

谢谢!

回答

4

我再次检查您的代码与工作之一。我认为NewCommit是你的活动。

除了(像意向意图=新意图(C,NewCommit.class);)明确设置在意向活动

我认为主要的区别(和问题)是你调用

PendingIntent.getBroadcast 

,而不是

PendingIntent.getActivity 

doc说:

“检索将启动新活动的PendingIntent,如调用Context.startActivity(意图)。请注意,该活动将在现有活动的上下文之外启动,因此您必须在Intent中使用Intent.FLAG_ACTIVITY_NEW_TASK启动标志。“

+0

感谢兄弟!您是对的!我花了数小时试图弄清楚这一点! ! 谢谢!! – Andres 2013-02-22 00:40:11