2012-11-28 17 views
0

我通過通知的意圖發送一些數據(字符串)。但是,當我嘗試通過通知發起的活動接收它時,我什麼也收不到。這是我的代碼。請幫忙。如何檢索通過意圖傳遞的數據?

這是通知代碼:

int icon=R.drawable.ic_launcher; 
       String ticket="Encrypted message"; 
       long when=System.currentTimeMillis(); 
       NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
       Context context1 =getApplicationContext(); 
       String expandedText="You've received an encrypted message, click to view"; 
       String expandedTitle="Encrypted Message"; 
       Notification notification = new Notification(icon, ticket, when); 
       Intent intent1=new Intent(context,Try.class); 
       intent1.putExtra("MSG","Jumbo"); 
       PendingIntent launchIntent=PendingIntent.getActivity(context,0, intent1,0); 

notification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent); 
       mNotificationManager.notify(1,notification); 

這裏是我如何接受它在Try.class:

t = (TextView)findViewById(R.id.dec); 
     String d = ""; 
     Intent I = getIntent(); 
     d = I.getStringExtra("MSG"); 
     String text = "This is an example"; 
     t.setText(d); 

沒有錯誤 - TextView的設置爲什麼都沒有,當我運行它

回答

0

getIntent()將無法在那裏工作,嘗試重寫您的活動中的onNewIntent方法。

0

而不是

Intent intent1=new Intent(context,Try.class); 

變化

Intent intent1=new Intent(YourActivity.this,Try.class); 

嘗試在onCreate()方法

​​
+0

它在'onCreate()'方法 – Saturnian

0

你曾經調用 的setResult(Activity.RESULT_OK,意圖) 你通知?您需要這樣或Android將永遠不會重新啓動您的「通話」活動。

+0

不,我沒有。我剛剛添加它,它說它不適用。 – Saturnian

+0

能否詳細說明一下?你是否收到語法錯誤,運行時錯誤或什麼? –

0

這應該工作,嘗試登錄「d」

Log.d("Value of D", d); 

我覺得你的問題是,你的TextView噸設置不正確,因此不顯示結果的值。

+0

我設置了d =「displayText」並執行它 - 它運行良好。 TextView沒問題!就是這個意圖! :( – Saturnian