2012-02-25 81 views
2

嗨,我是新來的機器人。 我正在實施具有通知功能的代碼。在這裏,我在我的應用程序中有兩項活動,分別是ActivityA和ActivityB。 我想從通知開始ActivityB,我需要向ActivityB發送一些標誌或一些值。我怎樣才能發送像int值或標誌的數據到所謂的活動使用通知點擊。問題是,當我從啓動器圖標首先啓動活動時,它將稱爲ActivityA,並從該ActivityA中傳遞一些值給ActivityB。 但是,當我從通知中啓動ActivityB時,我不會向該活動發送任何值,因此它正在強制關閉。如何在android中編寫onclick llistener通知項?

撥叫通知活動我使用這個代碼

mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis()); 
Context context = getApplicationContext(); 
CharSequence contentTitle = "Notification Details..."; 
CharSequence contentText = "Browse Android Official Site by clicking me";    
Intent notifyIntent = new Intent(Intent.ACTION_MAIN); 
notifyIntent.setComponent(new ComponentName("mypackage","mypackage.ActivityB")); 

PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent); 
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); 

請問我該如何從通知到名爲活動送價值。

回答

5

您必須設置ActivityB在notifyIntent

Intent notifyIntent = new Intent(this, ActivityB.class); // 'this' - Context object 

要發送的值使用額外 例如:

intent.putExtra("yourTag", yourVariable); 
+0

謝謝你的答覆。我可以通過getIntent.getExtras()獲取傳遞的值嗎?方法? – 2012-02-25 07:10:39

+0

是的,你可以做到。 – 2012-02-25 07:13:46

+0

謝謝你對我有幫助。 – 2012-02-25 08:55:45