Android Quick Development Series Common tool class

Source: Internet
Author: User

1. Log Tool class L.java

Package Com.way.common.util;import android.util.log;/** * LOG Unified Management class *  * @author-like *  */public class L{public stat IC Boolean isdebug = true;//If you need to print a bug, you can initialize the private static final String TAG = "means" in the OnCreate function of application;//The following four is the default TAG The function public static void I (String msg) {if (isdebug) log.i (TAG, msg);} public static void D (String msg) {if (isdebug) log.d (TAG, msg);} public static void E (String msg) {if (isdebug) log.e (TAG, msg);} public static void V (String msg) {if (isdebug) log.v (TAG, msg);} The following is the function that passed the custom tag public static void I (string tag, string msg) {if (isdebug) log.i (tag, msg);} public static void D (string tag, string msg) {if (isdebug) log.i (tag, msg);} public static void E (string tag, string msg) {if (isdebug) log.i (tag, msg);} public static void V (string tag, string msg) {if (isdebug) log.i (tag, msg);}}

See the class on the Internet, the note should be the original author's name, very simple one class; There are a lot of online to log on to the sdcard, but I have never recorded, so the introduction of the simplest, we can evaluate whether the need to expand ~ ~

2. Toast Unified Management Class

Package Com.way.common.util;import Android.content.context;import android.widget.toast;/** * Toast Unified Management class * */public Class T{public Static Boolean isshow = true;/** * Short time display toast * * @param context * @param message */public static void Sho Wshort (context context, charsequence message) {if (isshow) toast.maketext (context, message, Toast.length_short). Show () ;} /** * Short time display toast * * @param context * @param message */public static void Showshort (context context, int message) {if (IsS How) toast.maketext (context, message, Toast.length_short). Show (); /** * Long time display toast * * @param context * @param message */public static void Showlong (context context, charsequence message) {if (isshow) toast.maketext (context, message, Toast.length_long). Show (); /** * Long time display toast * * @param context * @param message */public static void Showlong (context context, int message) {if (issh ow) Toast.maketext (context, message, Toast.length_long). Show (); /** * Custom Display toast Time * * @param context * @param message * @param duration */PUBlic static void Show (context context, charsequence message, int duration) {if (isshow) toast.maketext (context, message, Du Ration). Show ();} /** * Custom Display toast Time * * @param context * @param message * @param duration */public static void Show (context context, int m essage, int duration) {if (isshow) toast.maketext (context, message, duration). Show ();}}

is also very simple one package, can save the province ~ ~

3, Sharedpreferences Packaging class Sputils

Package Com.zhy.utils;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.method;import Java.util.map;import Android.content.context;import Android.content.sharedpreferences;public Class SPUtils{/** * The file name stored in the phone */public static final String file_name = "Share_data";/** * method of saving data, we need to get the specific type of data to save, and then call different save method according to the type * * @para M context * @param key * @param object */public static void put (context context, String key, Object object) {Sharedpreferen Ces sp = context.getsharedpreferences (file_name,context.mode_private); Sharedpreferences.editor Editor = Sp.edit (); if (object instanceof String) {editor.putstring (key, (String) object);} else If (object instanceof Integer) {Editor.putint (key, (Integer) object);} else if (object instanceof Boolean) { Editor.putboolean (Key, (Boolean) object);} else if (object instanceof Float) {editor.putfloat (key, (Float) object);} else if (object instanceof Long) {Editor.putlong ( Key, (Long) object);} Else{editor.putstring (Key, object.tostring ());} ShAredpreferencescompat.apply (editor);} /** * Get the method to save the data, we based on the default value to save the specific type of data, and then call the relative method to get the values * * @param context * @param key * @param defaultobject * @return */publ IC Static Object Get (context context, String key, Object defaultobject) {sharedpreferences sp = Context.getsharedpreferenc ES (file_name,context.mode_private), if (Defaultobject instanceof String) {return sp.getstring (key, (String) Defaultobject);} else if (defaultobject instanceof Integer) {return Sp.getint (key, (Integer) defaultobject);} else if (Defaultobject Instan Ceof Boolean) {return Sp.getboolean (key, (Boolean) defaultobject),} else if (Defaultobject instanceof Float) {return Sp.getfloat (Key, (Float) defaultobject);} else if (defaultobject instanceof long) {return Sp.getlong (key, (Long) defaultobject);} return null;} /** * Remove the value already corresponding to a key value * @param context * @param key */public static void Remove (context context, String key) {Sharedpreferen Ces sp = context.getsharedpreferences (file_name,context.mode_private); Sharedpreferences.editor EDITor = Sp.edit (); Editor.remove (key); Sharedpreferencescompat.apply (editor);} /** * Clear All data * @param context */public static void clear (context context) {Sharedpreferences sp = Context.getsharedpreferen CES (file_name,context.mode_private); Sharedpreferences.editor Editor = Sp.edit (); Editor.clear (); Sharedpreferencescompat.apply (editor);} /** * Query If a key already exists * @param context * @param key * @return */public Static Boolean contains (context context, String key) { Sharedpreferences sp = context.getsharedpreferences (file_name,context.mode_private); return Sp.contains (key);} /** * Returns all key value pairs * * @param context * @return */public static map<string,?> GetAll (context context) {Sharedpreference s SP = context.getsharedpreferences (file_name,context.mode_private); return Sp.getall ();} /** * Create a compatible class that resolves the sharedpreferencescompat.apply method * * @author Zhy * */private Static class Sharedpreferencescompat{priva Te static Final method Sapplymethod = Findapplymethod ();/** * Reflection Find the method of apply * * @return */@SuppressWarnings({"Unchecked", "rawtypes"}) private static Method Findapplymethod () {Try{class clz = SharedPreferences.Editor.class; Return Clz.getmethod ("Apply");} catch (Nosuchmethodexception e) {}return null;} /** * If found, use apply to execute, otherwise use commit * * @param editor */public static void apply (Sharedpreferences.editor editor) {try{if (sAp Plymethod = null) {Sapplymethod.invoke (editor); return;}} catch (IllegalArgumentException e) {} catch (Illegalaccessexception e) {} catch (InvocationTargetException e) {} Editor.commit ();}}}

The use of Sharedpreference has made a proposal for encapsulation, the external release of Put,get,remove,clear and other methods;

Note that all of the commit operations are replaced with sharedpreferencescompat.apply in order to use apply instead of commit as much as possible

First of all, why, because the commit method is synchronous, and we often commit operations are in the UI thread, after all, is IO operation, as asynchronously as possible;

So we use apply to replace, apply asynchronous write;

But apply is equivalent to a commit is the new API, in order to better compatibility, we have done the matching;

Sharedpreferencescompat can also give you to create a compatible class to provide a certain reference ~ ~


Welcome Comments ~ ~ can continue to improve these classes ~



Android Quick Development Series Common tool class

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.