• Android学习笔记 6. Notification


    Android学习笔记

    Android基础开发——控件

    6. Notification

    通知

    6.1 两个对象

    Notification和NotificationManager

    NotificationManager类是一个通知管理器类,由系统维护的服务,是以单例模式的方式获得,一般不直接实例化这个类。

    在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationMansger,所以直接传递Context.NOTIFICATION_SERVICE即可。

    6.2 使用通知

    使用Builder构造器创建Notification对象

    使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。Android8.0新增了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示

    在这里插入图片描述

    NotificationChannel → 通知渠道:Android8.0引入,允许为要显示的每种通知类型创建用户自定义的渠道。

    通知重要程度:

    在这里插入图片描述

    属性含义
    NONE关闭通知
    MIN开启通知,不会弹出,没有提示音,状态栏不显示
    LOW开启通知,不弹出,没有提示音,状态栏显示
    DEFAULT开启通知,不弹出,有提示音,状态栏显示
    HIGH开启通知,会弹出,有提示音,状态栏中显示
    6.3 Notification对象的常见方法
    方法含义
    setContentTitle设置标题
    setContentText设置文本内容
    setSmallIcon设置小图标
    setLargeIcon设置通知的大图标
    setColor设置小图标的颜色
    setContentIntent设置点击通知后的跳转意图
    setAutoCancel设置点击通知后自动清除通知
    setWhen设置通知被创建的时间《默认当前时间》

    注意点:Android从5.0开始,所有应用程序的通知栏图标,应该只使用alpha图层进行绘制,不应该包括RGB,即不能带颜色

    6.4 演示
    //获取NotificationManager对象
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
    //判断版本
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ){
        NotificationChannel notificationChannel = new NotificationChannel("dingjiaxiong","测试通知",
                NotificationManager.IMPORTANCE_HIGH);
        manager.createNotificationChannel(notificationChannel);
    }
    
    //构建Notification对象
    Notification notification = new NotificationCompat.Builder(this,"dingjiaxiong")
            .setContentTitle("官方通知")
            .setContentText("年年有风,风吹年年")
            //小
            .setSmallIcon(R.drawable.ic_baseline_person_24)
            .build();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    
        <Button
            android:id="@+id/send_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发出通知"
            />
    
        <Button
            android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消通知"
            />
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    设置全局变量

    在这里插入图片描述

    设置点击事件

    //发出通知
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            manager.notify(1,notification);
        }
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    设置其他属性

    大图标

    在这里插入图片描述

    //大图标
    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.test1))
    
    • 1
    • 2

    在这里插入图片描述

    在这里插入图片描述

    创建pendingintent

    Intent intent = new Intent(this, NotificationActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    
    • 1
    • 2
    //构建Notification对象
    notification = new NotificationCompat.Builder(this, "dingjiaxiong")
            .setContentTitle("官方通知")
            .setContentText("年年有风,风吹年年")
            //小图标
            .setSmallIcon(R.drawable.ic_baseline_person_24)
            //大图标
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.test1))
            //设置小图标颜色
            .setColor(Color.parseColor("#ff0000"))
            //设置点击跳转意图
            .setContentIntent(pendingIntent)
            //设置点击通知后自动清除通知
            .setAutoCancel(true)
            .build();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    主动取消通知

    在这里插入图片描述

    id与上一个需要一致

  • 相关阅读:
    redis 搭建主从
    计算机视觉基础【OpenCV轻松入门】:获取图像的ROI
    共享剪切板小工具
    C++ opencv实现letterbox
    用摄像管替换电视机电路里的显像管的摄像机
    腾讯智能表格文档如何做进度管理
    玩转NAS | 打造一个动态网关,部署OpenResty - Nginx与Lua的强强联合
    gd407使用dm9000通讯异常
    java云端小区物业智能管理系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    appium操作微信小程序
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126206172