Android 消息通知栏用法

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

    <!--Toast按键布局-->
    <Button
        android:id="@+id/createNotify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="创建消息" />

    <Button
        android:id="@+id/deleteNotify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="删除消息" />

</LinearLayout>

MainActivity

package com.example.toastnotificationdemo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

public class MainActivity extends AppCompatActivity {

    private static final int NOTIFICATION_ID = 1;
    String id = "channelId";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        Button createNotify = findViewById(R.id.createNotify);
        Button deleteNotify = findViewById(R.id.deleteNotify);

        createNotify.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "创建消息", Toast.LENGTH_SHORT).show();
                Notification notification;
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //API>=26 (android 8.0)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    String name = "channelName";
                    NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
                    manager.createNotificationChannel(channel);

                    notification = new NotificationCompat.Builder(MainActivity.this, id)
                            .setChannelId(id)//通道ID
                            .setContentTitle("Notification的标题测试")//设置Notification在状态栏中的标题
                            .setContentText("Notification的内容测试")//设置Notification在状态栏中的内容
                            .setWhen(System.currentTimeMillis())//设置Notification启动时间
                            .setSmallIcon(R.drawable.sms)//设置Notification的在状态栏中的图标
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//消息大图标
                            .build();
                } else {
                    notification = new NotificationCompat.Builder(MainActivity.this, id)
                            .setContentTitle("Notification的标题测试")//设置Notification在状态栏中的标题
                            .setContentText("Notification的内容测试")//设置Notification在状态栏中的内容
                            .setWhen(System.currentTimeMillis())//设置Notification启动时间
                            .setSmallIcon(R.drawable.sms)//设置Notification的在状态栏中的图标
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                            .build();
                }
                // 发送Notification消息
                manager.notify(1, notification);

            }
        });
        deleteNotify.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "删除消息", Toast.LENGTH_SHORT).show();
                //获取系统的Notification服务
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //取消通知
                notificationManager.cancel(NOTIFICATION_ID);
            }
        });
    }
}

Android 消息通知栏用法

上一篇:Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)


下一篇:Android学习笔记之Dialog Notification