Setlatesteventinfo, Handler, SimpleDateFormat warning solutions in Android development

Source: Internet
Author: User
Tags locale

While using the Setlatesteventinfo () method under Notification when doing app development under Android 4.4.2 today, Eclipse prompts: "Type Notification is not recommended The method Setlatesteventinfo (context, charsequence, charsequence, pendingintent) "!

What is this for? After the query learned: Setlatesteventinfo This method has been deprecate, not recommended.

/**
* @hide
*/
Public Notification (context, int icon, charsequence tickertext, Long,
Charsequence Contenttitle, Charsequence contenttext, Intent contentintent)
{
This.when = when;
This.icon = icon;
This.tickertext = Tickertext;
Setlatesteventinfo (context, Contenttitle, ContentText,
Pendingintent.getactivity (context, 0, contentintent, 0));
}

This constructor is also deprecate by the Hide,setlatesteventinfo method and is not recommended, using Notification.builder.

In the 4.0.3 platform, API level 15, the notification setlatesteventinfo () function is also displayed as the Setlatesteventinfo () effect, viewing the document discovery, in API level 11, This function has been substituted and is deprecated.

Android under Setlatesteventinfo warning, handler warning, SimpleDateFormat warning

Notification in different versions of the use of a number of different, related to the use of builder, now most of the online data or API level 11 before the use of the introduction, if not familiar with, will be around some detours.

Now let's summarize the following and hope to help the programmers you use later.

The Setlatesteventinfo () function is the only implementation method below the API level 11 version, which is the Android 2.3.3 system. Previous related property settings no longer mentioned here, there is a lot of information on the Internet.

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 that are above API level 11 and are below API level (Android 4.1.2), you 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 have been invalid, to be set in 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 ();

The version that is higher than API level 16 can be 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 ();


"Note points":

There are many ways of writing notification in construction, but note that

Notification Notification = new Notification ();

This kind of construction method, must add Notification.icon this setting, otherwise, the program will not be an error, but would have no effect.

Also, add some caveats and solutions to the actual Android development:

1:handler

This Handler class should be static or leaks might occur:incominghandler
@SuppressLint ("Handlerleak")
Private Handler Mhandler = new Handler () {
@Override
public void Handlemessage (msg) {

};
};

Workaround:

Private Handler Mhandler = new Handler (new Handler.callback () {
@Override
public boolean handlemessage (msg) {
return false;
}
});


2:simpledateformat

To the local formatting use Getdateinstance (), getdatetimeinstance (), or
Gettimeinstance (), or use new SimpleDateFormat (String template, Locale
Locale) with a for example locale.us for ASCII dates.
@SuppressLint ("SimpleDateFormat")
SimpleDateFormat SimpleDateFormat = new SimpleDateFormat (
"Yyyy-mm-ddhh:mm:ss");
Workaround:

SimpleDateFormat Newsimpledateformat = new SimpleDateFormat (
"YYYY year mm month DD Day HH when mm points", Locale.getdefault ());

3:new HashMap ()

@SuppressLint ("Usesparsearrays")
public static Map Cmd_map = new HashMap ();

Warning Reason: Use new Sparsearray (...) instead for better performance

4: "String". toUpperCase (); "String". toLowerCase ();

@SuppressLint ("Defaultlocale")
Boolean b = "string". toUpperCase (). Equals ("string");

Workaround:

Boolean b = "string". Equalsignorecase ("string");

Warning Reason: Implicitly using the "default locale is a common source of Bugs:use touppercase (locale) instead

Related Article

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.