Android消息通知(notification)和PendingIntent传值

转自:http://www.cnblogs.com/zenfly/archive/2012/02/09/2343923.html

Android支持Toast和NotificationManager两种通知方式,前者相当于一个定时关闭的对话框,后者是在状态栏上显示一条消息。Toast和Notification都可以随时取消。
Toast 
A toast is a view containing a quick little message for the user. The toast class helps you create and show those. Toast的使用很简单:
Toast.makeText(this, "Service destroyed…", Toast.LENGTH_LONG).show();

NotificationManager

 
NotificationManager负责通知用户事件的发生。

 NotificationManager有三个公共方法:

 
1.    cancel(int id)    取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走.
2.    cancelAll()    取消以前显示的所有通知。
3.    notify(int id, Notification notification)     把通知持久的发送到状态条上.
//初始化NotificationManager:
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification代表着一个通知.

Notification的属性:
audioStreamType     当声音响起时,所用的音频流的类型
contentIntent     当通知条目被点击,就执行这个被设置的Intent.
contentView     当通知被显示在状态条上的时候,同时这个被设置的视图被显示.
defaults     指定哪个值要被设置成默认的.
deleteIntent     当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行.
icon     状态条所用的图片.
iconLevel     假如状态条的图片有几个级别,就设置这里.
ledARGB    LED灯的颜色.
ledOffMS    LED关闭时的闪光时间(以毫秒计算)
ledOnMS     LED开始时的闪光时间(以毫秒计算)
number     这个通知代表事件的号码
sound     通知的声音
tickerText    通知被显示在状态条时,所显示的信息
vibrate     振动模式.
when     通知的时间戳.    
Notification的公共方法:
describeContents()    Describe the kinds of special objects contained in this Parcelable's marshalled representation.
setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) 设置Notification留言条的参数
writeToParcel(Parcel parcel, int flags)    Flatten this notification from a parcel.
toString() …………….
 

 将Notification发送到状态条上:

复制代码
Notification notification = new Notification(R.drawable.icon, "Service started", System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Main.class), 0);

// must set this for content view, or will throw a exception

notification.setLatestEventInfo(this, "Test Service", "Service started", contentIntent);

nm.notify(R.string.hello, notification);
复制代码
Notification的取消
nm.cancel(R.string.hello);
完整代码实现
复制代码
public static void addNotificaction(String pId,String pTtitle,String pContent) {
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// 创建一个Notification
Notification notification = new Notification();
// 设置显示在手机最上边的状态栏的图标
notification.icon = R.drawable.icon;
// 当当前的notification被放到状态栏上的时候,提示内容
notification.tickerText = pTtitle;

/***
* notification.contentIntent:一个PendingIntent对象,当用户点击了状态栏上的图标时,该Intent会被触发
* notification.contentView:我们可以不在状态栏放图标而是放一个view
* notification.deleteIntent 当当前notification被移除时执行的intent
* notification.vibrate 当手机震动时,震动周期设置
*/
// 添加声音提示
notification.defaults=Notification.DEFAULT_SOUND;
// audioStreamType的值必须AudioManager中的值,代表着响铃的模式
notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;

//下边的两个方式可以添加音乐
//notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
//notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
Intent intent = new Intent(this, AndroidMain.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// 点击状态栏的图标出现的提示信息设置
notification.setLatestEventInfo(this, pTtitle, pContent, pendingIntent);
manager.notify(id, notification);
}
复制代码
Pendingintent传值问题
pendingintent传值经常获取到的值是第一次的值或者null,这个跟第二个参数和最后一个参数选择有关系。
PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

注:如果所要启动的Activity是单例模式,其传值方法请看onNewIntent调用时机

总结一下pendingIntent的常用FLAG标签

FLAG_ONE_SHOT:this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.

FLAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.

FLAG_CANCEL_CURRENT:if the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.

FLAG_UPDATE_CURRENT: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

上面4个flag中最经常使用的是FLAG_UPDATE_CURRENT,因为描述的Intent有更新的时候需要用到这个flag去更新你的描述,否则组件在下次事件发生或时间到达的时候extras永远是第一次Intent的extras。使用FLAG_CANCEL_CURRENT也能做到更新extras,只不过是先把前面的extras清除,另外FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT的区别在于能否新new一个Intent,FLAG_UPDATE_CURRENT能够新new一个Intent,而FLAG_CANCEL_CURRENT则不能,只能使用第一次的Intent。

另外两flag就比较少用,利用FLAG_ONE_SHOT获取的PendingIntent只能使用一次,再使用PendingIntent也将失败,利用FLAG_NO_CREAT获取的PendingIntent若描述的Intent不存在则返回NULL值.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android常驻通知(Notification)是指在用户状态栏中一直显示的通知图标和文本内容,不会因为用户操作或应用进程被销毁而消失。常驻通知通常用于实时监测、后台服务、音乐播放等需要持续提醒用户的场景。 常驻通知的实现步骤如下: 1. 首先,需要创建一个Notification对象,包括通知图标、标题、内容等信息。 2. 然后,创建一个PendingIntent,用于定义用户点击通知后的操作,比如打开应用的某个Activity或执行某个Service。 3. 创建一个NotificationChannel(通知渠道),用于定义通知的重要程度,包括声音、震动等设置。 4. 将Notification对象与PendingIntent关联,并将其设置为常驻通知的优先级。 5. 最后,调用NotificationManager的notify方法,将通知显示在用户的状态栏上。 需要注意的是,常驻通知存在一些使用限制和最佳实践: 1. 用户可以通过设置中的通知管理来关闭或打开特定应用的常驻通知。 2. 常驻通知不适合用于广告或频繁推送的内容,以免打扰用户。 3. 为了避免误导用户,常驻通知的图标和文本内容应与应用的实际情况相符。 4. 如果需要更新通知的内容或操作,可以使用NotificationManager的notify方法进行更新,并保持通知的id不变。 总之,常驻通知Android提供的一个重要功能,可以实现持续提醒用户和后台监测的需求。但应用开发者需要注意使用场景和用户体验,遵循Android的最佳实践,以确保用户对常驻通知的接受和理解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值