通知的代码:
1 NotificationManager mNotifiManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 2 Notification notification = new Notification(R.drawable.stat_sys_call_record, "停止", System.currentTimeMillis());//停止显示在状态 3 notification.flags = Notification.FLAG_AUTO_CANCEL; 4 Intent intent = new Intent(Intent.ACTION_VIEW); 5 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 6 7 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);//包装Intent
//下拉通知后显示,getString处表示的标题,而“Test”是小字正文 8 notification.setLatestEventInfo(this, getString(R.string.app_name), "Test", pendingIntent); 9 mNotifiManager.notify(620066, notification);
关于PendingIntent:
A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int)
,getBroadcast(Context, int, Intent, int)
, getService(Context, int, Intent, int)
; the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.
By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel()
to remove it.
关于它的FLAG,官方文档上也有解释:
Android中默认对PendingIntent的创建(如通过PendingIntent.getActivity方式)会进行优化检测,默认的情况下,新创建的PendingIntent如果和原先的基本一样,那么就会使用原先的PendingIntent。(这时候可以使用这四个参数控制PendingIntent的创建)