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