转自:http://blog.csdn.net/ddna/article/details/5122083
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
相关推荐
在Android系统中,发一个状态栏通知还是很方便的... NotificationManager是一个系统Service,必须通过getSystemService()方法来获取。 NotificationManagernm=(NotificationManager)getSystemService(NOTIFICATION_SERV
首先,发送一个状态栏通知必须用到两个类: NotificationManager 、 Notification。 NotificationManager : 是状态栏通知的管理类,负责发通知、清楚通知等。 NotificationManager 是一个系统Service,必须通过 ...
本文主要讲解状态栏通知Notification、NotificationManager,小编觉得非常详细,给大家一个参考,希望对大家学习有所帮助。
NULL 博文链接:https://forlan.iteye.com/blog/2264722
在使用手机时,当有未接来电或者新短消息时,手机会给...使用Notification和NotificationManager类发送和显示通知也比较简单,大致可以分为以下四个步骤 (1)调用getSystemService() 方法获取系统的NotificationManag
在使用手机时,当有未接来电或者是新短消息时,手机会给出...使用Notification和NotificationManager类发送和显示通知也比较简单,大致可分为以下4个步骤。 (1)调用getSystemService()方法获取系统的NotificationManag
关于通知Notification相信大家都不陌生了,平时上QQ的时候有消息来了或者有收到了短信,手机顶部就会显示有新消息什么的,就类似这种。今天就稍微记录下几种Notification...3.0以前的通知和3.0以后的通知是有些区别的。
android 状态栏的图标与文字提醒 NotificationManager与Notification
调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotifacationManager服务 通过构造器创建一个Notification对象 为Notification设置各种属性 通过NotificationManager发送Notification。
这个例子演示Android 在状态栏... NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(0, myNotification);//发送Notification
可以设置一条通知,当然可以更新一条通知,我们通过在调用NotificationManager.notify(ID, notification)时所使用的ID来更新它。为了更新你之前发布的通知,你需要更新或者创建一个NotificationCompat.Builder对象,...
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notification); or Json2Notification.from(context)....
自学 Android 做的第二十七个 Android 程序,使用了 NotificationManager、Notification 和 Notification.Builder
本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动、灯光、声音等效果,分享给大家供大家参考,具体内容如下 效果图: MainActivity: import java.io.File; import android....
要使用消息通知,必须要用到两个类:NotificationManager和Notification,其他NotificationManager的初始化是用getSystemService方法,并且通过notify方法来向android系统发送消息栏通知和显示。效果 :代码: 代码...
Android界面 NotificationManager使用Bitmap做图标,如何实现呢,本文将介绍解决方法,需要的朋友可以参考下
notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。