安卓状态栏通知Notification

Notification基本操作

Notification的基本操作主要有创建、更新、取消三种。一个Notification的必要属性有三项,如果不设置就会在运行时抛出异常:

  1. 小图标,通过setSmallIcon()方法设置
  2. 标题,通过setContentTitle()方法设置
  3. 内容,通过setContentText()方法设置
    除了这三个之外,还应该给Notification设置一个action,这样能直接跳转到其他app的某个activity、启动service或者发送broadcast,可以和用户进行交互。

创建Notification

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  1. 获取NotificationManager实例
  2. 实例化Notification.Builder并且设置属性
  3. 发送通知
 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                // 添加通知
                Notification.Builder notification1 = new Notification.Builder(MainActivity.this);
                // 设置通知图标
                notification1.setSmallIcon(R.drawable.advise2);
                // 设置通知标题
                notification1.setContentTitle("通知");
                // 设置通知内容
                notification1.setContentTitle("查看详细内容");
                // 查看详细内容自动关闭
                notification1.setAutoCancel(true);
                Intent intent = new Intent(MainActivity.this, com.example.notification.ContentActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                notification1.setContentIntent(pendingIntent);
                // 发送通知
                manager.notify(NOTIFYID_1, notification1.build());

取消notification

//                notificationManager.cancel(NOTIFYID_1);
            // // 清除ID号为常量NOTIFYID_1的通知
                // 清除全部通知
                notificationManager.cancelAll();

除了上面这一种还有另外的四种取消通知的方法:

  1. 点击通知栏的清除按钮,会清除所有可以清除的通知
  2. 设置了setAutoCancel() 或 FLAG_AUTO_CANCEL 的通知,点击该通知时会清除它
  3. 通过 NotificationManager 调用 cancel(int id) 方法清除指定 ID 的通知
  4. 通过 NotificationManager 调用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知

以下是部分代码

main.xml

这里创建两个button用于显示和清除通知

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示通知" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除通知" />
content.xml

用于显示通知

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/content"
        android:textAppearance="?android:attr/textAppearanceMedium" />
核心代码
package com.example.notification;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    final int NOTIFYID_1 = 124;     // 通知的ID
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取通知管理器,用于发送通知
        final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // 获取显示通知的按钮
        Button button1 = findViewById(R.id.button1);
        // 为显示通知的按钮添加点击事件监听器
        button1.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                // 添加通知
                Notification.Builder notification1 = new Notification.Builder(MainActivity.this);
                // 设置通知图标
                notification1.setSmallIcon(R.drawable.advise2);
                // 设置通知标题
                notification1.setContentTitle("通知");
                // 设置通知内容
                notification1.setContentTitle("查看详细内容");
                // 查看详细内容自动关闭
                notification1.setAutoCancel(true);
                Intent intent = new Intent(MainActivity.this, com.example.notification.ContentActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                notification1.setContentIntent(pendingIntent);
                // 发送通知
                manager.notify(NOTIFYID_1, notification1.build());
            }
        });
        // 获取删除通知按钮
        Button button2 = findViewById(R.id.button2);
        // 为删除通知按钮添加点击事件监听器
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                notificationManager.cancel(NOTIFYID_1);
            // // 清除ID号为常量NOTIFYID_1的通知
                // 清除全部通知
                notificationManager.cancelAll();
            }
        });

    }
}

以上只是简单的实现了任务栏的通知。

最终效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值