Use of notification under different apiLevels

Reprinted from: http://www.cnblogs.com/Arture/p/5523695.html


There are some differences in the use of Notification in different versions, involving the use of Builder. Now summarized as follows, I hope it will be helpful to programmers who will use it in the future.


 
In systems below API Level 11 , that is, systems below Android 2.3.3, the setLatestEventInfo() function is the only implementation method. The previous related property settings will not be mentioned here, there are many online materials.

 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); 

In systems higher than API Level 11 and lower than API Level 16 (Android 4.1.2), you can use Notification.Builder to construct functions. But you need to use getNotification() to implement notification. At this point, the Flags, icon and other attributes set in the notification in the previous version are all invalid and must be set in the 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();

For versions higher than API Level 16 , you can use the Builder and build() functions to facilitate the use of notification.

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


【important point】:

    There are many ways to write notification when constructing notification, but it should be noted that when using
  Notification notification = new Notification();
  this construction method, the notification.icon setting must be added, otherwise, the program will not report an error, but it will no effect. 


Problem:


When using the setLatestEventInfo() method under Notification , Eclipse prompts: "The method setLatestEventInfo(Context, String, String, PendingIntent) is undefined for the type Notification"!
Reason: The setLatestEventInfo method has been removed.
/**
* 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);
}


Guess you like

Origin blog.csdn.net/u012049463/article/details/67635150