"Go" "Android" Android different version under Notification creation method

Source: Internet
Author: User

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

/*** Constructs a Notification object with the information needed to* has a status bar icon without the standard Expande D view.**@paramicon The resource ID of the the icon to put in the status bar.*@paramTickertext the text that flows by in the status bar when the notification first* activates.*@paramWhen the time-to-show in the Time field. In the system.currenttimemillis* timebase.**@deprecatedUse {@linkBuilder} instead.*/@Deprecated PublicNotification (inticon, Charsequence Tickertext,LongWhen ) { 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  New Intent (this, mainactivity);   = 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.

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.

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 "!

/*** Sets the {@link#contentView} field to is 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>*@paramcontext the context for your application/activity.*@paramContenttitle The title that goes in the expanded entry.*@paramContentText The text, goes in the expanded entry.*@paramContentintent The intent to launch when the user clicks the expanded notification.* If It's an activity, it must I Nclude the* {@linkAndroid.content.intent#flag_activity_new_task} FLAG, which requires* that's take care of TASK management as Describ Ed in the* <a href= "{@Docroot}guide/topics/fundamentals/tasks-and-back-stack.html ">tasks and back* stack</a> document.**@deprecatedUse {@linkBuilder} instead.* @removed*/@Deprecated Public voidsetlatesteventinfo (Context context,charsequence contenttitle, charsequence ContentText, pendingintent contentintent) {Notification.builder Builder=NewNotification.builder (context);//First , ensure that key pieces of information, which may have been set directly//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.

Original address: http://www.cnblogs.com/Arture/p/5523695.html

"Go" "Android" Android different version under Notification creation method

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.