每个使用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中声明权限:

3、通知到来时闪烁LED灯

LED灯的使用涉及到以下一个属性:

ledARGB ——- 控制LED灯的颜色

ledOnMS ——- 控制LED灯亮起的时间,以毫秒为单位

ledOffMS ——? 控制LED灯熄灭的时间,以毫秒为单位

主要通过setLights()方法依次对这三个属性进行设置

setLights(Color.BLUE, 1000, 1000)

上面的代码就是让LED灯以蓝色一闪一闪

4、通知到来时以默认方式提醒

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

.setDefaults(Notification.DEFAULT_ALL)

设置默认值,由手机环境来决定在通知到来时播放什么铃声,如何振动,如何闪烁LED灯

最后说明一点,手机播放铃声、手机振动、LED灯的闪烁都需要真机调试,模拟器上是看不出效果的。

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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐