我正在开发一个 GPS 应用程序。如果用户进入或超出预定义区域,我的应用程序将通过 NotificationManager 触发通知。我的应用程序可以针对这两种情况触发通知。
从 onResume() 中,我总是得到最新的 intent.setExtra() 值,而不是点击通知的 intent.setExtra 值。
问题是如何检测用户点击内部区域或外部区域的通知?(我想在不同的情况下展示不同的观点)
是否可以为单击的通知添加侦听器?
这是我的代码spinet:
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");
}