Android · PendingIntent学习

Intent 是及时启动,intent 随所在的activity 消失而消失

PendingIntent用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。

PendingIntent 可以看作是对intent的包装,pendingintent中保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。

	private void showNotify(){
		Intent notificationIntent = new Intent(this,MainActivity.class); //点击该通知后要跳转的Activity
    	PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
    	Notification notice = new Notification.Builder(this)
        .setContentTitle("New mail from Man_hua")
        .setContentText("this is a test")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(contentIntent)
        .setAutoCancel(true)
        .setVibrate(new long[] { 100, 250, 100, 500 })
        .build();
    	
    	NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    	manager.notify(0,notice);
    }	
原文地址:https://www.cnblogs.com/manhua/p/4155031.html