首页 > 其他分享 >Android 使用通知

Android 使用通知

时间:2023-01-19 10:00:11浏览次数:38  
标签:NotificationManager val 通知 使用 参数 notification Android PendingIntent

通知(notification )是Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后可以看到通知的详细内容。

修改activity_main.xml 中的代码,如下所示:

<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/sendNotice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Notice" />
</LinearLayout>

修改 MainActivity 中的代码,如下所示:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 先需要一个NotificationManager 对通知进行管理,可以通过调用Context 的getSystemService()方法获取。
        // getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务,这里我们传入Context.NOTIFICATION_SERVICE即可。
        val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        // 由于NotificationChannel类和createNotificationChannel()方法都是Android 8.0 系统中新增的API,
        // 因此我们在使用的时候还需要进行版本判断才可以
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 创建一个通知渠道至少需要渠道ID、渠道名称以及重要等级这3个参数,
            // 其中渠道ID可以随便定义,只要保证全局唯一性就可以。
            // 渠道名称是给用户看的,需要可以清楚地表达这个渠道的用途。
            // 通知的重要等级主要有IMPORTANCE_HIGH、IMPORTANCE_DEFAULT、IMPORTANCE_LOW、IMPORTANCE_MIN这几种,对应的重要程度依次从高到低。
            val channel = NotificationChannel("normal", "Normal", NotificationManager.IMPORTANCE_DEFAULT)
            // 调用NotificationManager 的createNotificationChannel()方法完成创建。
            manager.createNotificationChannel(channel)
        }
        sendNotice.setOnClickListener {
            val intent = Intent(this, NotificationActivity::class.java)
            // PendingIntent 从名字上看起来就和Intent 有些类似,它们确实存在不少共同点。
            // 比如它们都可以指明某一个“意图”,都可以用于启动Activity 、启动Service 以及发送广播等。
            // 不同的是,Intent 倾向于立即执行某个动作,而PendingIntent 倾向于在某个合适的时机执行某个动作。
            // 所以,也可以把PendingIntent 简单地理解为延迟执行的Intent 。
            // 主要提供了几个静态方法用于获取PendingIntent 的实例,可以根据需求来选择是使用getActivity()方法、getBroadcast()方法,还是getService()方法。
            // 这几个方法所接收的参数都是相同的:第一个参数依旧是Context,不用多做解释;
            // 第二个参数一般用不到,传入0即可;
            // 第三个参数是一个Intent 对象,我们可以通过这个对象构建出PendingIntent 的“意图”;
            // 第四个参数用于确定PendingIntent 的行为,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT这4种值可选,通常情况下这个参数传入0就可以了。
            val pi = PendingIntent.getActivity(this, 0, intent, 0)
            // AndroidX库中提供了一个NotificationCompat类,使用这个类的构造器创建Notification对象
            // 第一个参数是context,这个没什么好说的;第二个参数是渠道ID,需要和我们在创建通知渠道时指定的渠道ID相匹配才行。
            val notification = NotificationCompat.Builder(this, "normal")
                .setContentTitle("This is content title")//指定通知的标题内容
                .setContentText("This is content text")//指定通知的正文内容
                .setSmallIcon(androidx.core.R.drawable.notification_icon_background)//设置通知的小图标
                .setLargeIcon(
                    BitmapFactory.decodeResource(
                        resources,
                        androidx.core.R.drawable.notification_icon_background
                    )
                )//设置通知的大图标
                .setContentIntent(pi)//通过PendingIntent 构建一个延迟执行的“意图”,当用户点击这条通知时就会执行相应的逻辑。
                .setAutoCancel(true)//setAutoCancel()方法传入true,就表示当点击这个通知的时候,通知会自动取消。
                .build()
            // 调用NotificationManager 的notify()方法就可以让通知显示出来了。
            // notify()方法接收两个参数:第一个参数是id,要保证为每个通知指定的id都是不同的;
            // 第二个参数则是Notification对象,这里直接将我们刚刚创建好的Notification对象传入即可。
            manager.notify(1, notification)
        }
    }

}

修改 activity_notification.xml 中的代码,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="This is notification layout"
        android:textSize="24sp" />
</RelativeLayout>

修改 NotificationActivity

class NotificationActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notification)

        /*val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        manager.cancel(1)//想取消哪条通知,在cancel()方法中传入该通知的id就行了。*/

    }
}

  


  

 

标签:NotificationManager,val,通知,使用,参数,notification,Android,PendingIntent
From: https://www.cnblogs.com/ooo0/p/17061091.html

相关文章

  • 使用cat命令注意事项,提前测试一下文件大小,否则size太大,会卡在终端或者命令行
      此.dbf文件大小太大,导致cat一直在输出,只有退出终端解决问题,cat应该要注意这个问题 ......
  • CentOS7下配置使用JumpServer 堡垒机 (图文教程)
    前面介绍了如何在《CentOS7下搭建JumpServer堡垒机》,基于这篇文章的环境搭建过程,接着介绍安装后的的功能配置使用。首次wbe登录,https://ip:80,默认账号密码:admin,admin;这......
  • Android 访问其他程序中的数据
    ContentProvider的用法一般有两种:一种是使用现有的ContentProvider读取和操作相应程序中的数据;另一种是创建自己的ContentProvider,给程序的数据提供外部访问接口。Con......
  • LRU 居然翻译成最近最少使用?真相原来是这样!
    前言相信有很多同学和我一样,第一次碰到LRU(LeastRecentlyUsed)的这个解释「最近最少使用」都不知道是什么意思,用汤老师的话来说:我真的感到匪夷所思啊!最近是表示时间......
  • 第四十九章 使用 ^SystemPerformance 监视性能 - 复制配置文件
    第四十九章使用^SystemPerformance监视性能-复制配置文件复制配置文件可以使用以下API命令将现有配置文件复制到具有不同名称的文件:setrc=$$copyprofile^System......
  • 使用PInvoke.net
    C#和C++的交互如果自己写代码,一方面繁琐,另一方面容易出错,再者就是代码不太规范。最近看了一下PInvoke.net的东西,可以直接使用官方写好的。下面是使用Pinvoke.net打开设备......
  • 如何使用sqlmap爆库和挂马
    ​这篇文章讲述一下sqlmap的基本使用,爆库和挂马。安装sqlmap: 这篇文章讲述一下sqlmap的基本使用,爆库和挂马。 windows:(1)安装python并配置环境PythonReleasesforWin......
  • 使用Graalvm加载Shellcode
    Graalvm介绍介绍Graalvm之前,首先就要了解Java编译的JIT和AOT是什么JIT(Just-in-Time,即时编译)和AOT(Ahead-of-Time,预编译),就像Java常见的是需要什么类,就加载进来编译并......
  • Vuw2和Vue3如何使用ref获取元素节点?
    Vue2中<template><divid="app"><divref="echohye">新年快乐</div></div></template><script>exportdefault{mounted(){console.log(this.$ref......
  • 使用 Burpsuite 测试的常用操作(一)
    大家好啊,我是大田。今天分享一下Burpsuite在工作中常用操作,本文先说说其中两个操作。一、了解一下Burpsuite做什么1、Burpsuite是一个黑客工具、安全测试工具、半......