Notification creation method under different versions of Android

Source: Internet
Author: User
Tags deprecated

Project Environment

Project Build target:android 6.0

Problem:

When using the new Notification (int icon, charsequence tickertext, long when) constructor, Eclipse prompts: "The constructor Notification (int , Charsequence, long) is deprecated "

The source code is as follows:

/*** constructs a Notification object with the information needed to* has a status bar icon without the standard expanded view.** @param icon The resource ID of the the icon to put in the status bar.* @param tickertext the text that flows by in th E 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.*/@Deprecatedpublic Notification (  int icon, charsequence tickertext, long when) {This.icon = icon;  This.tickertext = Tickertext; This.when = when;}

There are some differences in the use of notification under different versions, involving the use of builder. Now summarize the following and hope to help the programmers in the future.


Below API level 11, which is Android 2.3.3, the Setlatesteventinfo () function is the only implementation method. The previous related property settings are no longer mentioned here, there are many online data.

Intent  Intent = new Intent (this,mainactivity);  Pendingintent pendingintent = pendingintent.getactivity (context, 0, intent, pendingintent.flag_one_shot);  Notification.setlatesteventinfo (context, title, message, pendingintent);          

Higher than API level 11, a system that is lower than the API level (Android 4.1.2) version can use Notification.builder to construct functions. But use GetNotification () to make the notification implementation. At this point, the previous version of the notification set in the Flags,icon and other properties are invalid, to 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 ();

  Higher than the API level 16 version, you can use the builder and build () function to match the convenience 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 ();

"Note points":
There are many ways to do this when constructing notification, but be aware that
Notification Notification = new Notification ();
This construction method, must add Notification.icon this setting, otherwise, although the program will not error, but will not be effective.

Problem:

Using the Setlatesteventinfo () method under notification, Eclipse prompts: "The method Setlatesteventinfo (Context, String, String, pendingintent) is undefined for the type Notification"!

View Source:

/*** sets the {@link #contentView} field to is a view with the "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 of the context for Y Our application/activity.* @param contenttitle the title of the goes in the expanded entry.* @param contenttext the text t Hat goes in the expanded entry.* @param contentintent the intent-launch when the user clicks the expanded notification.  * If This was an activity, it must include the* {@link android.content.intent#flag_activity_new_task} FLAG, which requires* That's the care of the 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 */@ deprecatedpublic void Setlatesteventinfo (Context context,charsequence contenttitle, Charsequence ContentText, Pendingintent contentintent) {Notification.buIlder builder = new Notification.builder (context),//first, ensure that key pieces of information, the May has been set D irectly//is Preservedbuilder.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 Fieldsif (Contenttitle! = null) {builder.setcontenttitle (contenttitle);} if (contenttext! = null) {Builder.setcontenttext (contenttext);} Builder.setcontentintent (contentintent); Builder.buildinto (this);}

The Setlatesteventinfo method has been removed.

Notification creation method under different versions of Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.