我有一個BroadcastReceiver偵聽傳入的短消息。當新的短信到達時,它會顯示一條通知,用戶可以點擊它並打開該應用。我想將消息文本傳遞給我的活動。如何向intents發送附加內容?
在我的接收器:
Notification notifacation = new Notification();
notifacation.icon = icon;
notifacation.tickerText = tickerText;
notifacation.when = System.currentTimeMillis();
Bundle bundle = new Bundle();
bundle.putString("SMS_PARAMETERS", smsParameters);
Intent notificationIntent = new Intent(context, MyActivity.class);
notificationIntent.putExtra("SMS_PARAMETERS", bundle);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notifacation.setLatestEventInfo(context, tickerText, message, contentIntent);
notificationManager.notify(1, notifacation);
裏面我的活動:
@Override
public void onResume() {
Bundle bundle = this.getIntent().getExtras();
if(bundle != null) {
String smsParameters = bundle.getString("SMS_PARAMETERS");
Log.d(DEBUG_TAG, "SMS_PARAMETERS: " + smsParameters);
Toast.makeText(this, smsParameters, Toast.LENGTH_LONG).show();
}
super.onResume();
}
的問題是,束總是空。
有什麼想法?
之前,我想了很多,這是大大錯誤添加這一行。您不應該在onResume()中獲取包,否則notificationmanager會在不更改Bundle或setLatestEventInfo的情況下處理通知。你的代碼永遠不會顯示你真的將包傳遞給了意圖?我會回答你的問題,但我不確定你是否在問正確的事情。傳遞給intent:Intent i = new Intent(MYCLASS.class); i.putExtra(「KEY」,1); startActivity(ⅰ);然後在MYCLASS.class的onCreate中檢索額外的this.getIntent()。getExtras()。getInt(「KEY」); – bbedward 2012-03-06 20:56:40