Android Notification通知欄消息

Activity:


package com.wkk.app8;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button notification;
    private Button list;
    private Button myView;
    private Button big;
    private Button progress1;
    private Button progress2;
    private Button cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
        創建一個Notification必不可少的兩樣東西
        1.NotificationManager  控制Notification的展示和取消
        2.NotificationCompat.Builder  用於設置Notification的各種參數

        Notification點擊觸發的效果就是通過傳入的Intent來控制的
         */
        notification = (Button) findViewById(R.id.notification);
        list = (Button) findViewById(R.id.list);
        myView = (Button) findViewById(R.id.myView);
        big = (Button) findViewById(R.id.big);
        progress1 = (Button) findViewById(R.id.progress1);
        progress2 = (Button) findViewById(R.id.progress2);
        cancel = (Button) findViewById(R.id.cancel);

        notification.setOnClickListener(this);
        list.setOnClickListener(this);
        myView.setOnClickListener(this);
        big.setOnClickListener(this);
        progress1.setOnClickListener(this);
        progress2.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.notification:
                notificationshow();
                break;
            case R.id.list:
                listNotification();
                break;
            case R.id.myView:
                myviewNotification();
                break;
            case R.id.big:
                bigNotification();
                break;
            case R.id.progress1:
                showprogress1();
                break;
            case R.id.progress2:
                showprogress2();
                break;
            case R.id.cancel:
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.cancelAll();
                notificationManager.cancelAll();
                break;
        }
    }

    /**
     * 展示普通的
     */
    private void notificationshow() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(), PendingIntent.FLAG_ONE_SHOT);
        builder.setContentTitle("測試")//標題
                .setContentText("測試")//文本
                .setContentIntent(pendingIntent)//啓動intent
                .setNumber(10)//這個只是設置通知上的一個值,並不是創建10條notificationmanager   顯示數量
                .setTicker("提示文字")//提示文字
                .setWhen(System.currentTimeMillis())//時間
                .setPriority(Notification.PRIORITY_DEFAULT)//設置該通知優先級
                .setAutoCancel(false)//是否點擊取消
                .setOngoing(false)//於設置是否常駐通知欄,即是否可以側滑取消/刪除
                .setDefaults(Notification.DEFAULT_ALL)//用於設置提示聲音閃爍燈以及震動等等,
                .setSmallIcon(R.mipmap.ic_launcher);//圖標
        // 1.id,可以通過id取消Notifiation     2.Notification對象
        notificationManager.notify(1, builder.build());
    }

    /**
     * 自定義視圖-小
     */
    private void myviewNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(), PendingIntent.FLAG_ONE_SHOT);
        builder.setContentTitle("測試")//標題
                .setContentText("測試")//文本
                .setContentIntent(pendingIntent)//啓動intent
                .setAutoCancel(false)//是否點擊取消
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContent(new RemoteViews(getPackageName(), R.layout.myview));
        notificationManager.notify(2, builder.build());
    }

    /**
     * 大列表視圖
     */
    public void listNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setAutoCancel(true);
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        builder.setStyle(inboxStyle);
        inboxStyle.setBigContentTitle("測試大標題:");
        for (int i = 0; i < 10; i++) {
            inboxStyle.addLine(String.valueOf(i));
        }
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setContentIntent(pendingIntent);
        notificationManager.notify(3, builder.build());
    }

    /**
     * 自定義大視圖
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void bigNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        //無論如何這句代碼時不可或缺的
        builder.setSmallIcon(R.mipmap.ic_launcher); // 設置頂部圖標
        //創建視圖,普通view不行
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout);
        //點擊事件
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.button, pendingIntent);
        Notification notify = builder.build();
        notify.contentView = remoteViews; // 視圖
        notify.bigContentView = remoteViews; // 大視圖
        notify.flags = Notification.FLAG_ONGOING_EVENT;
        notificationManager.notify(4, notify);
    }

    /**
     * 不定長進度條
     */
    private void showprogress1() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(), PendingIntent.FLAG_ONE_SHOT);
        builder.setContentTitle("測試")//標題
                .setContentText("測試")//文本
                .setContentIntent(pendingIntent)//啓動intent
                .setNumber(10)//這個只是設置通知上的一個值,並不是創建10條notificationmanager   顯示數量
                .setTicker("提示文字")//提示文字
                .setWhen(System.currentTimeMillis())//時間
                .setPriority(Notification.PRIORITY_DEFAULT)//設置該通知優先級
                .setAutoCancel(false)//是否點擊取消
                .setOngoing(false)//於設置是否常駐通知欄,即是否可以側滑取消/刪除
                .setDefaults(Notification.DEFAULT_ALL)//用於設置提示聲音閃爍燈以及震動等等,
                .setSmallIcon(R.mipmap.ic_launcher)//圖標
                .setProgress(100, 30, true);
        // 1.id,可以通過id取消Notifiation     2.Notification對象
        notificationManager.notify(5, builder.build());
    }

    /**
     * 確定進度的進度條
     */
    private void showprogress2() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(), PendingIntent.FLAG_ONE_SHOT);
        builder.setContentTitle("測試")//標題
                .setContentText("測試")//文本
                .setContentIntent(pendingIntent)//啓動intent
                .setNumber(10)//這個只是設置通知上的一個值,並不是創建10條notificationmanager   顯示數量
                .setTicker("提示文字")//提示文字
                .setWhen(System.currentTimeMillis())//時間
                .setPriority(Notification.PRIORITY_DEFAULT)//設置該通知優先級
                .setAutoCancel(false)//是否點擊取消
                .setOngoing(false)//於設置是否常駐通知欄,即是否可以側滑取消/刪除
                .setDefaults(Notification.DEFAULT_ALL)//用於設置提示聲音閃爍燈以及震動等等,
                .setSmallIcon(R.mipmap.ic_launcher)//圖標
                .setProgress(100, 30, false);
        notificationManager.notify(6, builder.build());
        //如果要做下載就將 notificationManager和builder做成全局變量
        //在下載的過程中不斷調用builder.setProgress()和notificationManager.notify(1, builder.build());來更新進度
    }

}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <Button
        android:id="@+id/notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="普通" />


    <Button
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="列表" />

    <Button
        android:id="@+id/myView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定義視圖" />

    <Button
        android:id="@+id/big"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定義大視圖" />

    <Button
        android:id="@+id/progress1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="進度條不定長" />


    <Button
        android:id="@+id/progress2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="進度條定長" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清除" />


</LinearLayout>

權限

    <!-- 閃光燈權限 -->
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <!-- 振動器權限 -->
    <uses-permission android:name="android.permission.VIBRATE"/>

本案來還想寫點什麼,後來…忘記了…
全部代碼都在這裏了,就不上傳demo了


想起來忘了什麼了,效果圖…
普通:

列表:

自定義視圖:

自定義大視圖:

進度條不定長:

進度條定長:

end

發佈了71 篇原創文章 · 獲贊 80 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章