我想通過使用捆綁通過一個待處理意圖從我的活動從服務傳遞一些值。如果應用程序剛剛啓動,則一切正常,但當應用程序處於恢復模式時,儘管我的服務從遠程服務器接收到新值並將其置於待傳遞給活動,但該活動顯示舊值。下面是服務端的代碼: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