Android中通知Notification的使用方法_Android

每个使用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灯的闪烁都需要真机调试,模拟器上是看不出效果的。

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
, notification
通知
notification使用方法、android 通知使用权、android 通知的使用、android 通知栏使用、android notification,以便于您获取更多的相关知识。

时间: 2024-08-15 04:13:37

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

Android中通知Notification的使用方法

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

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

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

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中fragment嵌套fragment问题解决方法_Android

都说fragment好用,duang~~,又遇到问题了,记录一下,分享给遇到这个问题的同学! 1.fragment嵌套fragment时出现getActivity()为null         activity A嵌套fragment B,B嵌套fragment C,C跳转到activity D,当activity D被finish掉之后,C中容易爆出getActivity为空.如果你的activity被回收了,那你需要在bundle中保存一下fragment信息,我的解决方法:fragment

基于Android中手势交互的实现方法_Android

闲来无事,琢磨琢磨Android中的手势交互,发现网上在手势方面的文章并不是很多,而且很多的参考价值并不大.于是出此博文,与大家共勉.鉴于我写此博文时对手势交互的研究也不是特深,如果有不正确的地方,还请各位博友批评指正. 首先,在Android系统中,每一次手势交互都会依照以下顺序执行. 1. 接触接触屏一刹那,触发一个MotionEvent事件. 2. 该事件被OnTouchListener监听,在其onTouch()方法里获得该MotionEvent对象. 3. 通过GestureDetec

Android中实现HashMap排序的方法_Android

HashMap排序是数据结构与算法中常见的一种排序算法.本文即以Android平台为例来实现该算法. 具体代码如下: public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("lisi", 5); map.put("lisi1", 1); map.put("lisi2&quo

在android中增加curl的解决方法_Android

curl是一个著名的开源文件传输协议实现软件,其中包括了HTTP.HTTPS.FTP等多种常用不常用协议的实现.在curl最新版本的官方源代码中其实已经包括了android的编译文件(Android.mk),不过要想编译通过还需要做一些工作. 我所使用的curl版本是7.20.0,android源代码版本是eclair 2.1.首先将curl解压到external目录下,将目录名称改为curl(不改也可以,不过android的习惯是不带版本号,入乡随俗吧). (1) 首先要创建一个头文件curl

实例讲解Android中ContentProvider组件的使用方法_Android

ContentProvider基本使用为了在应用程序之间交换数据,android提供了ContentProvider,ContentProvider是不同应用程序之间进行数据交换的标准API,当一个应用程序需要把自己的数据暴露给其他程序使用时,该应用程序就可以通过提供ContentPRovider来实现,其他应用程序就可以通过ContentResolver来操作ContentProvider暴露的数据. 实现ContentProvider的步骤: 1)编写一个类,继承ContentProvide