PendingIntent 的使用 以及其中参数的理解

本博客内容:

  1. 点击通知,跳转到第二个aty,意图intent 使用 pendingIntent 封装

实例:

PendingIntent 的使用 以及其中参数的理解

参考代码

// 1. 设置 PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 2.将 PendingIntent 加入到 通知动作里
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                        .setContentTitle("标题")
                        .setContentText("内容")
                        .setSmallIcon(R.mipmap.ic_launcher_round)
                        .setContentIntent(pendingIntent)          // 此步为关键点
                        .setAutoCancel(true)
                        .setNumber(++messageNumber);

参数理解

PendingIntent几个常量:
  1. FLAG_CANCEL_CURRENT :如果AlarmManager管理的PendingIntent已经存在,那么将会取消当前的PendingIntent,从而创建一个新的PendingIntent
  2. FLAG_UPDATE_CURRENT:如果AlarmManager管理的PendingIntent已经存在,可以让新的Intent更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras,另外,我们也可以在PendingIntent的原进程中调用PendingIntent的cancel ()把其从系统中移除掉
  3. FLAG_NO_CREATE :如果AlarmManager管理的PendingIntent已经存在,那么将不进行任何操作,直接返回已经存在的PendingIntent,如果PendingIntent不存在了,那么返回null

以下为源代码,8.0要做兼容测试,所以此内容使用为Android 5.1 正常运行

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/startPendingIntentNotification"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动PendingIntent发送一个通知"
        android:onClick="start"
        />

</LinearLayout>

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个ATY" />
</android.support.constraint.ConstraintLayout>

Main2Activity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

MainActivity.java

package com.example.tnt.pendingintent;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {


    NotificationManager notificationManager;
    private int messageNumber = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void start(View view) {

        switch (view.getId()) {
            case R.id.startPendingIntentNotification:

                notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                Intent intent = new Intent(this, Main2Activity.class);

                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                        .setContentTitle("标题")
                        .setContentText("内容")
                        .setSmallIcon(R.mipmap.ic_launcher_round)
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true)
                        .setNumber(++messageNumber);

//                Notification notification = builder.build();


                notificationManager.notify(0, builder.build());


                break;
        }
    }
}