我有一个BroadcastReceiver,用于监听传入的短信。当收到新短信时,它会显示一条通知,通知用户可以单击它并打开该应用程序。我想将消息文本传递给我的活动。
在我的接收器内:
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();
}
问题在于捆绑包始终为空。
任何想法?
最佳答案
我看不到您要将捆绑软件添加到意图中的位置。您需要执行以下操作:
Bundle bundle = new Bundle();
bundle.putString("SMS_PARAMETERS", smsParameters);
Intent notificationIntent = new Intent(context, CarMapActivity.class);
notificationIntent.putExtras(bundle);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notifacation.setLatestEventInfo(context, tickerText, message, contentIntent);
notificationManager.notify(1, notifacation);
关于android - 如何使用Intents将额外内容发送到 Activity ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9591624/