天天看點

Android pendingIntent了解

pendingIntent

PendingIntent常用的靜态方法主要有:

  1. getActivitity(Context context, int requestCode,

    Intent intent, int flags) 跳轉到一個activity元件;

  2. getBroadcast(Context context, int requestCode,

    Intent intent, int flags) 打開一個廣播元件;

  3. getService(Context context, int requestCode,

    Intent intent,int flags)打開一個服務元件。

PendingIntent和Intent最大的差別就是PendingIntent的執行并不是立刻執行的。

最常用的地方就是在使用Notification的時候:

Notification.Builder builder = new Notification.Builder(this);
        // 必需的通知内容
builder.setContentTitle("content title")
                .setContentText("content describe")
                .setSmallIcon(R.mipmap.ic_launcher);

Intent notifyIntent = new Intent(this, NextActivity.class);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, , notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(notifyPendingIntent);
Notification notification = builder.getNotification();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(, notification);
           

其他的方法也是類似。

繼續閱讀