Notification和NotificationManager的基本用法
来源:互联网 发布:mac版finale免费30天 编辑:程序博客网 时间:2024/06/06 05:15
1. NotificationManager和Notification用来设置通知。
通知的设置等操作相对比较简单,基本的使用方式就是用新建一个Notification对象,然后设置好通知的各项参数,然后使用系统后台运行的NotificationManager服务将通知发出来。
基本步骤如下:
1)得到NotificationManager:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
2)创建一个新的Notification对象:
Notification notification = new Notification();
notification.icon = R.drawable.notification_icon;
也可以使用稍微复杂一些的方式创建Notification:
int icon = R.drawable.notification_icon; //通知图标
CharSequence tickerText = "Hello"; //状态栏(Status Bar)显示的通知文本提示
long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
Notification notification = new Notification(icon, tickerText, when);
3)填充Notification的各个属性:
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
Notification提供了丰富的手机提示方式:
a)在状态栏(Status Bar)显示的通知文本提示,如:
notification.tickerText = "hello";
b)发出提示音,如:
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)手机振动,如:
notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
d)LED灯闪烁,如:
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
4)发送通知:
private static final int ID_NOTIFICATION = 1;
mNotificationManager.notify(ID_NOTIFICATION, notification);
2. 通知的更新
如果需要更新一个通知,只需要在设置好notification之后,再调用setLatestEventInfo,然后重新发送一次通知即可。
3. 自定义通知视图
这部分可以参考官方文档,讲的很详细了。
AndroidSDK: docs/guide/topics/ui/notifiers/notifications.html
- Notification 和 NotificationManager 的基本用法
- Notification和NotificationManager的基本用法
- Notification和NotificationManager的基本使用方法
- Notification和NotificationManager的基本使用方法
- Notification和NotificationManager的基本使用方法
- Notification和NotificationManager的基本使用方法
- Notification和NotificationManager的基本使用方法
- Notification和NotificationManager的基本使用方法
- Notification和NotificationManager的基本使用方法
- 【Android笔记】Notification和NotificationManager的基本使用方法
- Android笔记】Notification和NotificationManager的基本使用方法(转)
- Android——Notification和NotificationManager的基本使用方法
- 【Android笔记】Notification和NotificationManager的基本使用方法
- Notification和NotificationManager的使用
- NotificationManager和Notification的使用总结
- NotificationManager和Notification的使用总结
- NotificationManager和Notification的使用总结
- NotificationManager和Notification的使用总结
- 常用的js小方法
- HTML:Hyper Text Markup Language 超文本标记语言
- Android权限大全(整理)
- Java设计模式透析之 —— 适配器(Adapter)
- PostgreSQL与MySQL比较
- Notification和NotificationManager的基本用法
- oracle 10g OEM服务启动不起来解决办法
- NodeJS用Express建立工程
- Android FoldingLayout 折叠布局 原理及实现(一)
- 如何使用PDF分割工具
- 多米音乐 获取歌词 抓包
- hbase 学习笔记
- CENTOS7下安装kermit超级终端
- 【java】解决httpurlconnection 乱码