Android Setlatesteventinfo warning, handler warning, SimpleDateFormat warning

Source: Internet
Author: User
Tags locale

Body


It's easy to use the Setlatesteventinfo () method under Notification when you're doing app development under Android 4.4.2, and Eclipse prompts: "The type is not recommended Notification Method Setlatesteventinfo (Context, Charsequence, Charsequence, pendingintent)"!

What is this for? Query after that: Setlatesteventinfo This method has been deprecate, is not recommended to use.


/**
* @hide
*/
Public Notification (context context, int icon, charsequence tickertext, long when,
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 for use with Notification.builder.

In the 4.0.3 platform, which is API level 15, when using the notification setlatesteventinfo () function, the setlatesteventinfo () effect is also displayed, viewing the document discovery, in the API Level 11, this function has been replaced, is not recommended for use.


In different versions of notification using a few different, related to change to the use of builder, now most of the online information is also the API level 11 before the introduction of the usage, if not familiar with, will detour some detours.

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);
Manager.notify (ID, notification);

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.

Also, add some caveats and workarounds for actual Android development:
1:handler
This Handler class should is static or leaks might occur:incominghandler
@SuppressLint ("Handlerleak")
Private Handler Mhandler = new Handler () {
@Override
public void Handlemessage (Message msg) {

};
};

Workaround:

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


2:simpledateformat

To get 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 mm month DD Day hh mm", 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


Reference
1, notification in different versions of the use of tips
2. Resolve handler warning, SimpleDateFormat warning, "String" in Android. toUpperCase () warning

Android Setlatesteventinfo warning, handler warning, SimpleDateFormat warning

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.