notification中发起activity问题
String ns = Context.NOTIFICATION_SERVICE;?
? ? NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);?
? ? int icon = R.drawable.icon; ? ? ?
? ? long when = System.currentTimeMillis();?
? ? Notification notification = new Notification(icon, "Test Notification", when);?
?
?
? ? Context context = getApplicationContext(); ? ? ??
?
? ? Bundle bundle = new Bundle();?
? ? bundle.putString("action", "view");?
? ? Intent notificationIntent = new Intent(this, MainActivity.class);?
? ? notificationIntent.putExtras(bundle);?
?
? ? PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);?
? ? notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); ? ? ?
? ? mNotificationManager.notify(1, notification);?
?
下面是要取出"action", 的值
Bundle bundle = this.getIntent().getExtras();?
?
? ? if(bundle != null)?
? ? {?String action = bundle.getString("action");?
? ? ? ? ? ? performAction(action)?
? ? }?
程序呢按照预期执行,只是当我返回程序,把状态栏缩小,然后回到屏幕通过程序的图标发起程序竟然从上次notivication点击的时候发起,
? ? ? ?
可以通过
Intent intent = getIntent();?
int flags = intent.getFlags();?
boolean launchedFromHistory = ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)
来判断一下是不是从历史中发起的