Android中通知Notification的使用方法

每个使用Android手机的人应该对Android中的通知不陌生,下面我们就学习一下怎么使用Android中的通知。

一、通知的基本用法

活动、广播接收器和服务中都可以创建通知,由于我们一般在程序进入后台后才使用通知,所以真实场景中,一般很少在活动中创建通知。

1、第一行代码上面介绍的创建通知的方法

//获得通知管理器 NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) //创建通知对象,参数依次为通知图标、ticker(通知栏上一闪而过的信息)、通知创建时间 Notification notification = new Notification(R.drawable. ic_launcher, "This is ticker text", System.currentTimeMillis()); //设置通知布局,参数依次为Context,通知标题、通知正文、PindingIntent对象(点击通知之后的事件处理) notification.setLatestEventInfo(this, "This is content title", "This is content text", null); //显示通知,参数依次为唯一的id、通知对象 manager.notify(1, notification);

注:上面的方法现在已经被废弃,当API Level为11及之前时使用此方法

2、APILevel高于11低于16的可以用下面的方法创建通知

//1、获得通知管理器 NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); //创建Builder,设置属性 Notification.Builder builder = new Notification.Builder(this) .setAutoCancel(true) .setContentTitle("title") .setContentText("describe") .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) .setOngoing(true); //获得Notification对象 Notification notification = builder.getNotification(); //显示通知 manager.notify(1, notification);

3、API Level在16及以上,使用下面的方法创建通知

//1、获得通知管理器 NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); //创建Builder,设置属性 Notification notification = new Notification.Builder(this) .setAutoCancel(true) .setContentTitle("title") .setContentText("describe") .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) .setOngoing(true) .build(); //显示通知 manager.notify(1, notification);

二、响应通知的点击事件

我们通过 PendingIntent对象响应容通知的点击事件
 1、获得PendingIntent对象

PendingIntent用来处理通知的“意图”。我们需要先构造一个Intent对象,然后再通过PendingIntent.getActivity()、PendingIntent.gBroadcast()、PendingIntent.getService()来启动执行不同的意图。这三个静态方法传入的参数相同,第一个为Context,第二个参数一般传入0,第三个参数为Intent对象,第四个参数指定PendingIntent的行为,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT和FLAG_UPDATE_ CURRENT这四种值可选。

2、设置PendingIntent

通过setContentIntent(pendingIntent)来设置。

下面是一个简单的例子

//获得通知管理器 NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); //构造Intent对象 Intent intent = new Intent(MainActivity.this, TestActivity.class); //获得PendingIntent对象 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //创建Builder,设置属性 Notification notification = new Notification.Builder(this) .setAutoCancel(true) .setContentTitle("title") .setContentText("describe") .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(pendingIntent) //设置PendingIntent .setWhen(System.currentTimeMillis()) .setOngoing(true) .build(); //显示通知 manager.notify(1, notification);

三、取消通知

取消通知只需要在cancel()方法中传入我们创建通知时指定的id即可 
复制代码 代码如下:NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(1);

四、通知的高级用法

1、通知到来时播放音频

Notification有一个属性是sound,这里需要传入音频对应的URI

//音频Uri Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones")); setSound(soundUri);

2、通知到来时手机振动

我们使用vibrate这个属性让通知到来时控制手机振动。vibrate需要一个长整型数组,用于设置手机静止和振动的时长,单位为毫秒。下标为偶数的表示手机静止的时长,下标为奇数为手机振动的时长。

//手机振动静止设置(静止0秒,振动一秒,静止一秒,振动一秒) long[] vibrate = {0, 1000, 1000, 1000}; setVibrate(vibrate)

注:控制手机还需要在AndroidManifest.xml中声明权限:

<uses-permission android:name="android.permission.VIBRATE"/>

3、通知到来时闪烁LED灯

LED灯的使用涉及到以下一个属性:
ledARGB ——- 控制LED灯的颜色
ledOnMS ——- 控制LED灯亮起的时间,以毫秒为单位
ledOffMS ——– 控制LED灯熄灭的时间,以毫秒为单位
主要通过setLights()方法依次对这三个属性进行设置

setLights(Color.BLUE, 1000, 1000)

上面的代码就是让LED灯以蓝色一闪一闪
4、通知到来时以默认方式提醒

如果我们不想手动设置这么多属性,可以使用下面的方式 
.setDefaults(Notification.DEFAULT_ALL)

设置默认值,由手机环境来决定在通知到来时播放什么铃声,如何振动,如何闪烁LED灯
最后说明一点,手机播放铃声、手机振动、LED灯的闪烁都需要真机调试,模拟器上是看不出效果的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-09-20 07:50:47

Android中通知Notification的使用方法的相关文章

Android中通知Notification的使用方法_Android

每个使用Android手机的人应该对Android中的通知不陌生,下面我们就学习一下怎么使用Android中的通知. 一.通知的基本用法 活动.广播接收器和服务中都可以创建通知,由于我们一般在程序进入后台后才使用通知,所以真实场景中,一般很少在活动中创建通知. 1.第一行代码上面介绍的创建通知的方法 //获得通知管理器 NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_

Android中通知Notification使用实例(振动、灯光、声音)_Android

本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动.灯光.声音等效果,分享给大家供大家参考,具体内容如下 效果图: MainActivity: import java.io.File; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; im

Android中通知Notification使用实例(振动、灯光、声音)

本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动.灯光.声音等效果,分享给大家供大家参考,具体内容如下 效果图: MainActivity: import java.io.File; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; im

Android中new Notification创建实例的最佳方法_Android

目前 Android 已经不推荐使用下列方式创建 Notification实例: Notification notification = new Notification(R.drawable.ic_launcher,"This is ticker text",System.currentTimeMillis()); 最好采用下列方式: Notification notification = new Notification.Builder(this) .setContentTitle

Android中new Notification创建实例的最佳方法

目前 Android 已经不推荐使用下列方式创建 Notification实例: Notification notification = new Notification(R.drawable.ic_launcher,"This is ticker text",System.currentTimeMillis()); 最好采用下列方式: Notification notification = new Notification.Builder(this) .setContentTitle

Android中3种全屏方法及3种去掉标题栏的方法

这篇文章主要介绍了Android中3种全屏方法及3种去掉标题栏的方法,二个问题各给出了3种解决方法,并给出实例代码,需要的朋友可以参考下     一.去掉标题栏的方法 第一种:入门的时候经常使用的一种方法 代码如下: requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉标题栏注意这句一定要写在setContentView()方法的前面,不然会报错的 第二种:在AndroidManifest.xml文件中定义 代码如下: <application

android中获取root权限的方法以及原理(转)

一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的"越狱"有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有掌控手机系统的权限,比如删除一些system/app下面的无用软件,更换开关机铃声和动画,拦截状态栏弹出的广告等. 2. Root的原理介绍 谷歌的android系统管理员用户就叫做root,该帐户拥有整个系统至高无上的权利,它可以访问和修改你手机几乎所有的文件,只有root才具备最高级别

图片-在android中使用Runtime.getRuntime().exec()方法

问题描述 在android中使用Runtime.getRuntime().exec()方法

Android中修改设备权限的方法_Android

本文实例讲述了Android中修改设备权限的方法.分享给大家供大家参考.具体如下: 有时我们编写了驱动后,在上层程序中要访问设备,但android代码编译后的设备权限是root的,其他用户不可访问(包括system),只是就需要在android源码中将设备的权限修改下. 具体的修改位置为源码的system/core/init/devices.c文件中static struct perms_ devperms[]的定义中,如添加设备hidraw0的权限,只需添加一行: 复制代码 代码如下: { "