我正在開發GPS應用程序。如果用戶進入預定義區域內部或外部,我的應用程序將通過NotificationManager觸發通知。我的應用可以針對這兩種情況啓動通知。如何檢測由NotificationManager發送的通知的發件人/檢測通知欄上的哪個通知已被點擊?
從onResume(),我總是得到最新的intent.setExtra()值,而不是intent.setExtra值的點擊通知的值。
的問題是如何檢測對內部區域或區外通知用戶點擊? (我想在不同的情況下顯示不同的觀點)
是否可以添加一個偵聽器來通知點擊?
這裏是我的代碼尖晶石:
private void displayNotificationMessage(String message, boolean vibrate, boolean playSound, Intent contentIntent, String notificationTag)
{
Notification notification = new Notification(R.drawable.buslogo, message, System.currentTimeMillis());
PendingIntent myPendingIntent = PendingIntent.getActivity(this.getBaseContext(),0, contentIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, "Friendly Reminder", message, myPendingIntent);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (vibrate)
notification.vibrate=new long[] {100L, 100L, 200L, 500L};
if (playSound)
notification.defaults |= Notification.DEFAULT_SOUND;
notification.number = ++notificationCounter;
notificationMgr.notify(notificationTag, notificationCounter, notification);
}
@Override
protected void onNewIntent(Intent intent) {
Log.i(TAG, "*********** onNewIntent(), intent = " + intent);
if (intent.getExtras() != null)
{
Log.i(TAG, "in onNewIntent = " + intent.getExtras().getString("test"));
}
super.onNewIntent(intent);
setIntent(intent);
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "*********** Main - onResume()");
Intent intent = this.getIntent();
if (intent.getExtras() != null)
{
Log.i(TAG, "test = " + intent.getExtras().getString("test"));
}
}
public void createNotification(String msnContent)
{
Intent intent = new Intent();
intent.setClass(this, Main.class);
Bundle bundle = new Bundle();
bundle.putString("test", msnContent);
intent.putExtras(bundle);
displayNotificationMessage(msnContent, true, true, intent, "test");
}
謝謝,我修改了我的代碼。每次在通過notificationMgr.notify()發送通知之前,我都會將一個新的PendingIntent對象傳遞給notification.setLatestEventInfo()。但它仍然無法區分通知欄上的哪個通知已被點擊。由於onResume()總是返回最新的setExtra()值。 – 2011-05-24 16:39:54
是否有任何方式將價值/對象從原始活動傳遞給通知所調用的意圖? – 2011-05-24 16:42:44
@ user767987:「由於onResume()總是返回最新的setExtra()值。」請參閱http://stackoverflow.com/questions/6089698/get-the-intent-that-woke-up-my-activity/6089739#6089739 – CommonsWare 2011-05-24 16:47:09