Notification通知

Notification:
例子的NotificationCompat使用的是v4包下的,稍微有點老,若是你們使用的話,建議使用v7包下的,通知也沒什麼好介紹的,直接看代碼
MainActivity:android

package com.fitsoft;

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

public class MainActivity extends AppCompatActivity {

    private int notificationId = 100;
    private static int times = 0;
    Button button;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;  //設置上下文對象

        button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                times++;
                if(times%2 == 1){
                    button.setText("銷燬通知");
                    createNotification();  //建立通知
                }else {
                    button.setText("建立通知");
                    destroyNotification(); //取消通知
                }
            }
        });
    }

    /**
     * 建立通知
     */
    private void createNotification() {
        //建立builder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "my_notifications");
        //設置屬性
        builder.setSmallIcon(R.mipmap.ic_launcher_round);  //設置圖標
        builder.setContentTitle("這是通知的標題");     //設置標題
        builder.setContentText("這是通知的具體內容");  //設置內容
        builder.setNumber(1);    //設置通知次數
        builder.setAutoCancel(true);   //設置是否自動取消
        //構建啓動意圖
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1,
                new Intent(this, SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        //設置啓動意圖
        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();
        //獲取系統服務
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //系統建立通知
        notificationManager.notify(notificationId, notification);
    }

    /**
     * 取消通知
     */
    private void destroyNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //取消通知
        notificationManager.cancel(notificationId);
    }
}

佈局文件使用的是上一次的只有一個按鈕的狀態選擇器的佈局,具體請查看上一篇。
這裏的方法都很好理解,只是其中有一個構建啓動意圖的時候的PendingIntent.getActivity()方法,咱們具體看一下:app

/**
 * Retrieve a PendingIntent that will start a new activity, like calling
 * {@link Context#startActivity(Intent) Context.startActivity(Intent)}.
 * Note that the activity will be started outside of the context of an
 * existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK
 * Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.
 *
 * <p class="note">For security reasons, the {@link android.content.Intent}
 * you supply here should almost always be an <em>explicit intent</em>,
 * that is specify an explicit component to be delivered to through
 * {@link Intent#setClass(android.content.Context, Class) Intent.setClass}</p>
 *
 * @param context The Context in which this PendingIntent should start
 * the activity.
 * @param requestCode Private request code for the sender
 * @param intent Intent of the activity to be launched.
 * @param flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE},
 * {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT},
 * or any of the flags as supported by
 * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts
 * of the intent that can be supplied when the actual send happens.
 *
 * @return Returns an existing or new PendingIntent matching the given
 * parameters.  May return null only if {@link #FLAG_NO_CREATE} has been
 * supplied.
 */
public static PendingIntent getActivity(Context context, int requestCode,
        Intent intent, @Flags int flags) {
    return getActivity(context, requestCode, intent, flags, null);
}

這裏的第四個參數flags值得注意,咱們點擊這個@Flags註解進行查看:ide

/** @hide */
@IntDef(flag = true,
        value = {
                FLAG_ONE_SHOT,
                FLAG_NO_CREATE,
                FLAG_CANCEL_CURRENT,
                FLAG_UPDATE_CURRENT,
                FLAG_IMMUTABLE,

                Intent.FILL_IN_ACTION,
                Intent.FILL_IN_DATA,
                Intent.FILL_IN_CATEGORIES,
                Intent.FILL_IN_COMPONENT,
                Intent.FILL_IN_PACKAGE,
                Intent.FILL_IN_SOURCE_BOUNDS,
                Intent.FILL_IN_SELECTOR,
                Intent.FILL_IN_CLIP_DATA
        })
@Retention(RetentionPolicy.SOURCE)
public @interface Flags {}

這是一個源碼級的註解,有五個經常使用的值以及一些Intent的經常使用值,這五個值的含義大體以下:佈局

  1. FLAG_ONE_SHOT:代表這裏構建的PendingIntent只能使用一次
  2. FLAG_NO_CREATE:若是前一個PendingIntent已經不存在,將再也不構建它
  3. FLAG_CANCEL_CURRENT:若是構建的PendingIntent已經存在,則取消前一個,從新構建一個
  4. FLAG_UPDATE_CURRENT:若是構建的PendingIntent已經存在,則替換它
  5. FLAG_IMMUTABLE:已經構建的PendingIntent不可更改

這裏咱們使用第4個,由於它比較經常使用。
效果圖:ui

相關文章
相關標籤/搜索