下面我有两个通知的代码,并且在意图我正在传递额外内容key1
,key2
但NotificationPillDescription.class
我只能使用 getExtras 获取 key1。
// 第一次通知的通知代码
Notification notification = new Notification(R.drawable.ic_launcher, "Take your morning pill!", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notifyIntent = new Intent(getApplicationContext(), NotificationPillDescription.class);
Bundle extras = new Bundle();
extras.putString("Key1", String.valueOf(1));
notifyIntent.putExtras(extras);
contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, 0);
notification.setLatestEventInfo(this, "Take your Morning pill!", "My Drug", contentIntent);
notificationManager.notify(dataProvider.notificationId++, notification);
// 第二次通知的通知代码
notifyIntent = new Intent(getApplicationContext(), NotificationPillDescription.class);
Bundle extras = new Bundle();
extras.putString("Key2", String.valueOf(2));
notifyIntent.putExtras(extras);
contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, 0);
notification.setLatestEventInfo(this, "Take your Morning pill!", "My Drug", contentIntent);
notificationManager.notify(dataProvider.notificationId++, notification);
//NotificationPillDescription.class 的代码
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.containsKey(String.valueOf(1)))
{
Log.d("Pill Id0",extras.getString(String.valueOf(1)) );
}
else
{
Log.d("Pill Id1",extras.getString(String.valueOf(2)) );
}
}