8.0 Notification的使用1. 普通的notification 2. 带进度条的notification 3. 自定义的notification,点击事件进入软件的一个activity

实现功能:
1. 普通的notification2. 带进度条的notification3. 自定义的notification,点击事件进入软件的一个activity

最终效果:

activity_main.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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

<Button
    android:id="@+id/showNormalNotification"
    android:text="发送 普通 通知"
    android:onClick="showNotification"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <Button
    android:id="@+id/showProgressNotification"
    android:text="发送 带进度条 通知"
    android:onClick="showNotification"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <Button
    android:id="@+id/showCustomNotification"
    android:text="发送 自定义 通知"
    android:onClick="showNotification"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


</LinearLayout>

activity_notification.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=".NotificationActivity">

    <TextView
        android:text="我是自定义的"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>

layout_custom_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
android:id="@+id/turn"
    android:orientation="vertical">

    <TextView
        android:id="@+id/notification_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="notification_title" />

    <TextView
        android:id="@+id/notification_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="notification_content" />

</LinearLayout>

MainActivity.java

package com.example.tnt.notificationchannel;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import static android.app.Notification.VISIBILITY_SECRET;

public class MainActivity extends AppCompatActivity {

    private NotificationManager manager;

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

    }

    public void showNotification(View view) {

        switch (view.getId())
        {
            case R.id.showNormalNotification:
                sendNormalNotification();
                break;
            case R.id.showProgressNotification:
                sendProgressNotification();
                break;
            case R.id.showCustomNotification:
                sendCustomNotification();
                break;
        }
    }

    // 3. ------------自定义 布局 和 交互
    private void sendCustomNotification(){
        Notification.Builder builder = getNotificationBuilder();
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.layout_custom_notification);
        remoteViews.setTextViewText(R.id.notification_title,"魔道祖师官方");
        remoteViews.setTextViewText(R.id.notification_content,"魏无羡和蓝忘机结婚了,喵喵喵"); // 如果不自己在这设置,就会默认布局里的设置

        Intent intent = new Intent(this,NotificationActivity.class);
        // 即将发生的意图,可以被取消可以被更新。
        PendingIntent pendingIntent = PendingIntent.getActivity(this,-1,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.turn,pendingIntent);

//        耗资源的意图
//        remoteViews.setOnClickFillInIntent(R.id.turn,intent);

        builder.setCustomContentView(remoteViews);
//
        getManager().notify(3,builder.build());

    }
    // 2. ---------- 进度条的Notification-------------
    private void sendProgressNotification() {
        final Notification.Builder builder = getNotificationBuilder();
        builder.setOnlyAlertOnce(true);  // 只提示一次声音
        getManager().notify(2, builder.build());
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    try {
                        Thread.sleep(100);
                        builder.setProgress(100, i, false);
                        getManager().notify(2, builder.build());

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();
    }


    // 管理者 是否存在
    private NotificationManager getManager() {
        if (manager == null) {
            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return manager;
    }
    // 发送普通通知
    private void sendNormalNotification() {
        Notification.Builder builder = getNotificationBuilder();
        getManager().notify(1, builder.build());
    }
    // 检验 兼容性检验 是否是 Android 8.0以上
    private Notification.Builder getNotificationBuilder() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
            channel.canBypassDnd();   //可否绕过勿扰模式
            channel.enableLights(true);  // 能否闪光
            channel.setLockscreenVisibility(VISIBILITY_SECRET); // 锁屏显示通知
            channel.setLightColor(Color.RED);  // 闪光灯颜色
            channel.canShowBadge(); // 桌面 lanucher 消息角标
            channel.enableVibration(true); // 能否震动
            channel.getAudioAttributes();// 获取系统响铃声音的配置
            channel.getGroup(); // 获取通知取到道组
            channel.setBypassDnd(true); // 可以绕过勿打扰模式
            channel.setVibrationPattern(new long[]{100, 100, 100}); // 震动的时间间隔
            channel.shouldShowLights();   // 是否闪光
            getManager().createNotificationChannel(channel); // 相当于 manger.createNotificationChannel(channel);

        }

        return new Notification.Builder(this)
                .setAutoCancel(true)
                .setChannelId("channel_id")
                .setContentTitle("每日新闻")
                .setContentText("明天是周六")
                .setSmallIcon(R.mipmap.ic_launcher_round)
//                .setStyle(new Notification.BigTextStyle())
                ;
    }



}

NotificationActivity.java

package com.example.tnt.notificationchannel;

import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class NotificationActivity extends AppCompatActivity {

    private NotificationManager manager;

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

        // 创建之后清除 通知
        getManager().cancel(3);
    }


    //1.-----------
    private NotificationManager getManager() {
        if (manager == null) {
            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return manager;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38340601/article/details/82926034