• 【转】【Android】Android不同版本下Notification创建方法


    使用 new Notification(int icon, CharSequence tickerText, long when)构造函数时,Eclipse却提示:" The constructor Notification(int, CharSequence, long) is deprecated "

    /**
    * Constructs a Notification object with the information needed to
    * have a status bar icon without the standard expanded view.
    *
    * @param icon The resource id of the icon to put in the status bar.
    * @param tickerText The text that flows by in the status bar when the notification first
    * activates.
    * @param when The time to show in the time field. In the System.currentTimeMillis
    * timebase.
    *
    * @deprecated Use {@link Builder} instead.
    */
    @Deprecated
    public Notification(int icon, CharSequence tickerText, long when)
    {
      this.icon = icon;
      this.tickerText = tickerText;
      this.when = when;
    }

    在不同的版本下Notification使用有一些不同,涉及到Builder的使用。现在总结如下,希望对以后使用的程序员有所帮助。

     
      低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。

    Intent  intent = new Intent(this,MainActivity);  
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
    notification.setLatestEventInfo(context, title, message, pendingIntent);          
    manager.notify(id, notification); 

    高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。

    Notification.Builder builder = new Notification.Builder(context)  
                .setAutoCancel(true)  
                .setContentTitle("title")  
                .setContentText("describe")  
                .setContentIntent(pendingIntent)  
                .setSmallIcon(R.drawable.ic_launcher)  
                .setWhen(System.currentTimeMillis())  
                .setOngoing(true);  
    notification=builder.getNotification();

    高于API Level 16的版本,就可以用Builder和build()函数来配套的方便使用notification了。

    Notification notification = new Notification.Builder(context)    
             .setAutoCancel(true)    
             .setContentTitle("title")    
             .setContentText("describe")    
             .setContentIntent(pendingIntent)    
             .setSmallIcon(R.drawable.ic_launcher)    
             .setWhen(System.currentTimeMillis())    
             .build();

    【注意点】:
        在构造notification的时候有很多种写法,但是要注意,用
      Notification notification = new Notification();
      这种构建方法的时候,一定要加上notification.icon这个设置,不然,程序虽然不会报错,但是会没有效果。 

    问题:

    使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“ The method setLatestEventInfo(Context, String, String, PendingIntent) is undefined for the type Notification”!

    /**
    * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
    * layout.
    *
    * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
    * in the view.</p>
    * @param context The context for your application / activity.
    * @param contentTitle The title that goes in the expanded entry.
    * @param contentText The text that goes in the expanded entry.
    * @param contentIntent The intent to launch when the user clicks the expanded 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);
    
    // First, ensure that key pieces of information that may have been set directly
    // are preserved
    builder.setWhen(this.when);
    builder.setSmallIcon(this.icon);
    builder.setPriority(this.priority);
    builder.setTicker(this.tickerText);
    builder.setNumber(this.number);
    builder.setColor(this.color);
    builder.mFlags = this.flags;
    builder.setSound(this.sound, this.audioStreamType);
    builder.setDefaults(this.defaults);
    builder.setVibrate(this.vibrate);
    builder.setDeleteIntent(this.deleteIntent);
    
    // now apply the latestEventInfo fields
    if (contentTitle != null) {
    builder.setContentTitle(contentTitle);
    }
    if (contentText != null) {
    builder.setContentText(contentText);
    }
    builder.setContentIntent(contentIntent);
    builder.buildInto(this);
    }

    setLatestEventInfo方法已被removed。

    原文地址:http://www.cnblogs.com/Arture/p/5523695.html

  • 相关阅读:
    delphi 数据导出 进度条自己生成
    在存储过程中编写正确的事务处理代码(SQL Server 2000 & 2005)
    在Delphi中如何获得SQL中存储过程的返回值?
    object-c中管理文件和目录:NSFileManager使用方法
    Delphi中动态链接库(DLL)的建立和使用
    开源免费天气预报接口API以及全国所有地区代码!!(国家气象局提供)
    wince 程序无法执行的原因
    如果我写一个播放器
    busybox配置
    switch case被人忽视的一点!zt
  • 原文地址:https://www.cnblogs.com/mqxs/p/7501276.html
Copyright © 2020-2023  润新知