`
tansitongba
  • 浏览: 483297 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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

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 假如状态条的图片有几个级别,就设置这里.
ledARGBLED灯的颜色.
ledOffMSLED关闭时的闪光时间(以毫秒计算)
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值.


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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics