PendingIntent传递数据注意参数RequestCode和Flag
数据发送方:
public static void notify(Context context, TxrjMessage msg) {
NotificationManager notifiMgr = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
/*Notification notifi = new Notification(R.drawable.sms_icon,
"短信", System.currentTimeMillis()); */
Notification notifi = new Notification();
notifi.icon = R.drawable.sms_icon;
notifi.tickerText = msg.getBody();
notifi.when = System.currentTimeMillis();
/*notifi.flags = Notification.FLAG_ONGOING_EVENT
| Notification.FLAG_NO_CLEAR | Notification.FLAG_SHOW_LIGHTS;*/
notifi.defaults = Notification.DEFAULT_ALL;
Intent it = new Intent(context, MessageListActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String displayName = TextUtils.isEmpty(msg.getName()) ? msg.getNumber() : msg.getName();
it.putExtra(TxrjConstant.EXTRA_THREAD_DISPLAY_NAME, displayName);
it.putExtra(TxrjConstant.EXTRA_THREAD_NUMBER, msg.getNumber());
notificationId++;
it.putExtra(TxrjConstant.EXTRA_NOTIFICATION_ID, notificationId);
PendingIntent pendIt = PendingIntent.getActivity(context,
TxrjConstant.REQUEST_READ_NOTIFICATION,
it, PendingIntent.FLAG_UPDATE_CURRENT);
notifi.setLatestEventInfo(context, displayName, msg.getBody(), pendIt);
notifiMgr.notify(notificationId, notifi);
}
数据接收方:
mThreadId = getIntent().getLongExtra(TxrjConstant.EXTRA_THREAD_ID, -1);
mDisplayName = getIntent().getStringExtra(TxrjConstant.EXTRA_THREAD_DISPLAY_NAME);
mName = getIntent().getStringExtra(TxrjConstant.EXTRA_THREAD_NAME);
mNumber = getIntent().getStringExtra(TxrjConstant.EXTRA_THREAD_NUMBER);
int notifiId = getIntent().getIntExtra(TxrjConstant.EXTRA_NOTIFICATION_ID, -1);
if(notifiId != -1) {
NotificationManager notifiMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notifiMgr.cancel(notifiId);
}
如果使用PendingIntent.FLAG_UPDATE_CURRENT,那么每次notifiId都是相同的数字,说明PendingIntent是一个,旧的参数被更新了。
如果使用PendingIntent.FLAG_ONE_SHOT,那么PendingIntent只是第一次有效,后来再点击别的Notification就无效了。
是否和getActivity的第2个参数有关系呢?
确实如此,第2个参数如果不同,那么表示PendingIntent不同,就不会出现上述两种情况。
将发送方蓝色部分代码改成
PendingIntent pendIt = PendingIntent.getActivity(context,
notificationId, it,
PendingIntent.FLAG_ONE_SHOT);
这里使用FLAG_ONE_SHOT是正确有效的。如日志所示。
07-21 02:59:52.154: E/txrjsms(17085): ShowSmsActivity.onDestroy
07-21 02:59:57.749: E/txrjsms(17085): MessageListActivity.onCreate
07-21 02:59:57.910: I/txrjsms(17085): notifiId:1
07-21 02:59:58.260: E/txrjsms(17085): MessageListActivity.onStart
07-21 02:59:58.260: E/txrjsms(17085): MessageListActivity.onResume
07-21 03:00:01.353: E/txrjsms(17085): MessageListActivity.onPause
07-21 03:00:01.943: E/txrjsms(17085): MessageListActivity.onStop
07-21 03:00:01.943: E/txrjsms(17085): MessageListActivity.onDestroy
07-21 03:00:04.536: E/txrjsms(17085): MessageListActivity.onCreate
07-21 03:00:04.726: I/txrjsms(17085): notifiId:2
07-21 03:00:04.956: E/txrjsms(17085): MessageListActivity.onStart
07-21 03:00:04.956: E/txrjsms(17085): MessageListActivity.onResume
07-21 03:00:06.748: E/txrjsms(17085): MessageListActivity.onPause
07-21 03:00:07.279: E/txrjsms(17085): MessageListActivity.onStop
07-21 03:00:07.279: E/txrjsms(17085): MessageListActivity.onDestroy
07-21 03:00:09.541: E/txrjsms(17085): MessageListActivity.onCreate
07-21 03:00:09.591: I/txrjsms(17085): notifiId:3
07-21 03:00:09.941: E/txrjsms(17085): MessageListActivity.onStart
07-21 03:00:09.951: E/txrjsms(17085): MessageListActivity.onResume
07-21 03:00:12.213: E/txrjsms(17085): MessageListActivity.onPause
07-21 03:00:12.844: E/txrjsms(17085): MessageListActivity.onStop
07-21 03:00:12.844: E/txrjsms(17085): MessageListActivity.onDestroy
将FLAG_ONE_SHOT改FLAG_UPDATE_CURRENT测验一下!
PendingIntent pendIt = PendingIntent.getActivity(context,
notificationId, it,
// PendingIntent.FLAG_ONE_SHOT);
PendingIntent.FLAG_UPDATE_CURRENT);
下面的日志表示:FLAG_UPDATE_CURRENT照样OK!
07-21 03:07:03.034: E/txrjsms(18079): MessageListActivity.onCreate // 准备发送短信
07-21 03:07:03.174: I/txrjsms(18079): notifiId:-1
07-21 03:07:03.224: E/txrjsms(18079): MessageListActivity.onStart
07-21 03:07:03.224: E/txrjsms(18079): MessageListActivity.onResume
07-21 03:07:15.757: E/txrjsms(18079): MessageListActivity.onPause
07-21 03:07:17.268: E/txrjsms(18079): MessageListActivity.onStop
07-21 03:07:17.268: E/txrjsms(18079): MessageListActivity.onDestroy // 关闭信息列表界面
07-21 03:08:01.621: E/txrjsms(18079): ShowSmsActivity.onDestroy // 关闭新信息的弹窗
07-21 03:09:39.917: E/txrjsms(18079): MessageListActivity.onCreate // 查看第3个短信通知
07-21 03:09:40.047: I/txrjsms(18079): notifiId:3
07-21 03:09:40.378: E/txrjsms(18079): MessageListActivity.onStart // onCreate之后就是onStart
07-21 03:09:40.378: E/txrjsms(18079): MessageListActivity.onResume
07-21 03:10:28.835: E/txrjsms(18079): MessageListActivity.onPause // 展开状态栏
07-21 03:10:28.835: E/txrjsms(18079): MessageListActivity.onNewIntent // 查看第2个短信通知
07-21 03:10:28.835: I/txrjsms(18079): notifiId:2
07-21 03:10:28.965: E/txrjsms(18079): MessageListActivity.onResume // onNewIntent之后就是onResume
07-21 03:12:48.401: E/txrjsms(18079): MessageListActivity.onPause // 展开状态栏
07-21 03:12:48.401: E/txrjsms(18079): MessageListActivity.onNewIntent // 查看第1个短信通知
07-21 03:12:48.401: I/txrjsms(18079): notifiId:1
07-21 03:12:48.561: E/txrjsms(18079): MessageListActivity.onResume // onNewIntent之后就是onPause
07-21 03:14:31.242: E/txrjsms(18079): MessageListActivity.onPause // 点击HOME键
07-21 03:14:31.712: E/txrjsms(18079): MessageListActivity.onStop
07-21 03:15:02.332: E/txrjsms(18079): MessageListActivity.onRestart // 点击应用图标,启动栈顶的那个Activity
07-21 03:15:02.332: E/txrjsms(18079): MessageListActivity.onStart
07-21 03:15:02.332: E/txrjsms(18079): MessageListActivity.onResume
07-21 03:16:38.286: E/txrjsms(18079): MessageListActivity.onPause // 点击BACK键
07-21 03:16:38.826: E/txrjsms(18079): MessageListActivity.onStop
07-21 03:16:38.826: E/txrjsms(18079): MessageListActivity.onDestroy