我有一个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