<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">使用Notification,必须了解三个类:</span>
import android.app.PendingIntent;
import android.app.NotificationManager;
import android.app.Notification;
首先先来看一段代码,根据这段代码来具体学习这三个类:
NotificationManager manager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "注意了,我出现在状态栏上了";
notification.defaults=Notification.DEFAULT_SOUND;
notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pendingIntent);
manager.notify(1, notification);
这里把三个类全部使用上了,下面就具体介绍下每个类的使用方法:
NotificationManager,一个系统后台服务,用于显示,关闭Notification。
获取实例:
NotificationManager nm = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
使用方法:
很简单,明了,不再多说。
Notification,
设置 Notification 的各种属性:
//设置通知在状态栏显示的图标
m_Notification.icon = R.drawable.icon;
//当我们点击通知时显示的内容
m_Notification.tickerText = "Button1 通知内容.....";
//通知时发出的默认声音
m_Notification.defaults = Notification.DEFAULT_SOUND;
//设置通知显示的参数
Intent m_Intent = new Intent(this, DesActivity.class);
PendingIntent m_PIntent = PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);
m_Notification.setLatestEventInfo(this, "Button1", "Button1通知", m_PIntent );
//这个可以理解为开始执行这个通知
m_NotificationManager.notify(0, m_Notification);
提醒的更新
每一个提醒都对应有一个唯一标识符ID,在发布提醒时指定的:
NotificationManager.notify(ID, notification)
在更新提醒的时候,只需要在修改Notification的一些属性之后,再调用其setLatestEventInfo(),然后重新发送一次通知即可。
Notification丰富的提示方式
a) tickerText :显示文本提示
notification.tickerText = "hello";
b) sound :发出提示音
notification.defaults |= Notification.DEFAULT_SOUND;
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
c) vibrate :手机振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
d) ledARGB 、ledOnMS 、ledOffMS :LED灯闪烁
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
声音提醒
·使用默认声音
notification.defaults |= Notification.DEFAULT_SOUND;
·使用自定义声音
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
·注:如果定义了默认声音,那么自定义声音将被覆盖
振动提醒
·使用默认振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
·使用自定义振动
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
·注:如果定义了默认振动,那么自定义振动将被覆盖
灯光闪烁提醒
·使用默认闪烁
notification.defaults |= Notification.DEFAULT_LIGHTS;
·使用自定义闪烁
notification.ledARGB = 0xff00ff00; // LED灯的颜色,绿灯
notification.ledOnMS = 300; // LED灯显示的毫秒数,300毫秒
notification.ledOffMS = 1000; // LED灯关闭的毫秒数,1000毫秒
notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志
更多特性
可以通过 Notification 的相关字段或标志(flags)为提醒设置更多的特性。
·FLAG_AUTO_CANCEL 标志:当提醒被用户点击之后会自动被取消(cancel);
·FLAG_INSISTENT 标志:在用户响应之前会一直重复提醒音;
·FLAG_ONGOING_EVENT 标志:Add this to the flags field to group the notification under
the "Ongoing" title in the Notifications window.
·FLAG_NO_CLEAR 标志:当在提醒栏中点击“清除提醒”按钮时,该提醒将不会被清除;
PendingIntent