当前位置: 移动技术网 > 移动技术>移动开发>Android > Android中通知Notification的使用方法

Android中通知Notification的使用方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网