我想通过使用捆绑通过一个待处理意图从我的活动从服务传递一些值。如果应用程序刚刚启动,则一切正常,但当应用程序处于恢复模式时,尽管我的服务从远程服务器接收到新值并将其置于待传递给活动,但该活动显示旧值。下面是服务端的代码:Android:无法从重新启动服务替换捆绑
private void sendNotification(String wholemsg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
/* Do something to extract salesID and notificationmessage from wholemsg
....
....
....
salesID=....
notificationmessage=...
....
*/
Bundle bundle=new Bundle();
bundle.putString("msg", notificationmessage);
bundle.putString("strsalesID", salesID);
notificationIntent.replaceExtras(bundle);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_ONE_SHOT+PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("AppV001 Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationmessage))
.setContentText(notificationmessage);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
这是对我的活动onRestart方法:
@Override
protected void onRestart() {
super.onRestart();
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(1);
try {
Intent intent = getIntent();
if (intent.hasExtra("msg") && intent.hasExtra("strsalesID")) {
String strmsgtitle = intent.getStringExtra("msg");
salesID = intent.getStringExtra("strsalesID");
titletext.setText(getString(R.string.str_ettitle) + salesID);
}
} catch (Exception ex) {
return;
}
}
的问题是,salesID保持之前的值时,应用程序来自隐藏模式回来。看起来,该服务在隐藏时无法更改活动包。
非常感谢您的时间!
根据什么launchMode你已经在你的清单这项活动的规定,你可能。需要重写['onNewIntent()'](http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent))以从服务接收更新的'Intent' – acj
感谢您回复!我尝试使用onNewIntent()但它不会触发事件。这里是清单: intent-filter> –
user2723342