首页 > 其他 > 详细

形形色色的通知(三)——番外

时间:2015-11-23 00:40:23      阅读:446      评论:0      收藏:0      [点我收藏+]

  前面关于Notification的介绍已经能完成大部分的功能,这篇主要记录一下,看到但是暂时还没用的上知识点和功能。

  一、对于一个app,notification的区别是由id或者id与tag组成的一个对,作为唯一的notification标识。

 * Each of the notify methods takes an int id parameter and optionally a
 * {@link String} tag parameter, which may be {@code null}.  These parameters
 * are used to form a pair (tag, id), or ({@code null}, id) if tag is
 * unspecified.  This pair identifies this notification from your app to the
 * system, so that pair should be unique within your app.  If you call one
 * of the notify methods with a (tag, id) pair that is currently active and
 * a new set of notification parameters, it will be updated.  For example,
 * if you pass a new status bar icon, the old icon in the status bar will
 * be replaced with the new one.  This is also the same tag and id you pass
 * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear
 * this notification.

 

  从中,我们可以看到,对于新建,还是更新已有通知,Android并不是用方法去区分,而是用notification的id去区分的

 

  二、对于新建与更新notification

  在Notification类中,有一个内部类Builder,这个类能构帮我们构造通知。

  eg:

    NotificationManager notificationManager;
    Notification.Builder notificationBuilder;
    Notification notification;
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationBuilder = new Notification.Builder(this);
    notificationBuilder.setVibrate(new long[]{100,200,100,200});
    notificationBuilder.setContentTitle("hi~");
    notification = notificationBuilder.build();

  如上的代码即是利用 Notification.Builder创建了一个Notification,并设置了它的振动和Title;有了这个Notification之后,就可以轻松地:

  notificationManager.notify(111 , notification);

 

  其中111就是我随便敲的id。

  notificationBuilder中还有很多属性可以设置,在此不一一列举(以后可能会加在附录)。

  上面说的是新建。

  如果我想修改notification的属性,再重发一次。网上有些博客写的方法是

notification.setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent);

  我在Notification的代码中还能找到这个方法,但是这个方法已经被废弃了。

 

     * If this is an activity, it must include the
     * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
     * that you take care of task management as described in the
     * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
     * Stack</a> document.
     *
     * @deprecated Use {@link Builder} instead.
     * @removed
     */
    @Deprecated
    public void setLatestEventInfo(Context context,
            CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
        Notification.Builder builder = new Notification.Builder(context);

 

  新的版本中,修改Notification依旧使用Builder类。build一个新的notification,只要id和tag一样,notify()出去的也只是更新当前的。

 

暂时这些,学到再加。。。

形形色色的通知(三)——番外

原文:http://www.cnblogs.com/fishbone-lsy/p/4987110.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!