Android Notification的作用和用法

10 篇文章 0 订阅
7 篇文章 0 订阅
Notification是一种让你的应用程序在不使用Activity的情况下警示用户。Notification由NotificationManger统一管理,目前包含的能力有:

1.创建一个状态条图标。



2.在扩展的状态条窗口中显示额外的信息(和启动一个Intent)。



3.闪灯或LED。



4.电话震动



 5.发出听得见的警告声(铃声,保存的声音文件)。



Notification是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。
作为UI部分,Notification对移动设备来说是最适合不过的了。用户可能随时都带着手机在身边,但不会随时都关心它或者程序。一般来说,用户会在后台打开几个程序,但他们不会注意它们。
在这样的情形下,当发生需要注意的事件时,能够通知用户是很重要的。
Notification可以通过紧急的重复或者(最常用)在状态条使用一个图标来通知用户。状态条图标可以正常的更新或者通过扩展的状态条窗口来显示额外的信息,显示扩展的状态条窗口,点击状态条图标并将其拖到屏幕底部。“锁定”位置,确保你在松开拖拽前窗口已经覆盖整个屏幕。隐藏它,简单的拖回去即可。

NotificationManager介绍

NotificationManager是一个系统服务,用于处理Notification。使用getSystemService方法可以得到它的一个引用,如下面的代码所示:

String svcName = Context.NOTIFICATION_SERVICE;

NotificationManager notificationManager;

notificationManager = (NotificationManager)getSystemService(svcName);

使用NotificationManager,你可以触发新的Notification,修改已经存在的或者移除那些不再需要或想要的Notification。

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. 自定义通知视图


   这部分可以参考官方文档,讲的很详细了。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值