Android学习笔记--常用工具类

来源:互联网 发布:淘宝手持身份证照片 编辑:程序博客网 时间:2024/05/17 02:47

常用工具类

自定义Application继承系统Application

package rc.loveq.p2ptest.common;import android.app.Activity;import android.app.Application;import android.content.Context;import android.os.Handler;import android.os.Process;import java.util.LinkedList;import java.util.List;import java.util.ListIterator;/** * Author:Rc * Csdn:http://blog.csdn.net/loveqrc * 0n 2017/1/7 22:05 * Email:664215432@qq.com */public class MyApplication extends Application {    public static Context sApplicationContext;    public static  Handler sHandler;    public static Thread mThread;    public static int mMainThreadId;    private List<Activity> activitys = new LinkedList<Activity>();    @Override    public void onCreate() {        sApplicationContext = getApplicationContext();        sHandler = new Handler();        mThread = Thread.currentThread();        mMainThreadId = Process.myTid();    }    public void addActivity(Activity activity) {        activitys.add(activity);    }    public void removeActivity(Activity activity) {        activitys.remove(activity);    }    public void closeApplication() {        closeActivitys();    }    private void closeActivitys() {        ListIterator<Activity> iterator = activitys.listIterator();        while (iterator.hasNext()) {            Activity activity = iterator.next();            if (activity != null) {                activity.finish();            }        }    }}

UIUtil

public class UIUtil {    public static Context getContext() {        return MyApplication.sApplicationContext;    }    public static Handler getHandler() {        return MyApplication.sHandler;    }    public static int getColor(int colorId) {        return getContext().getResources().getColor(colorId);    }    public static View getXmlView(int layoutId) {        return View.inflate(getContext(), layoutId, null);    }    public static String[] getStringArray(int stringArr) {        return getContext().getResources().getStringArray(stringArr);    }    public static int dp2px(int dp) {        float density = getContext().getResources().getDisplayMetrics().density;        return (int) (dp * density + 0.5);    }    public static int px2dp(int px) {        float density = getContext().getResources().getDisplayMetrics().density;        return (int) (px / density + 0.5);    }    private static Toast toast;    /**     * 避免多次显示     * @param content     */    public static void showToast(String content) {        if (toast == null) {            toast = Toast.makeText(getContext(),                    content,                    Toast.LENGTH_SHORT);        } else {            toast.setText(content);        }        toast.show();    }    public static boolean isMainThread(){        int tid = android.os.Process.myTid();        return tid== MyApplication.mMainThreadId;    }    public static boolean isMainThread(){        int tid = Process.myTid();        return tid==MyApplication.mMainThreadId;    }}

MD5

public class MD5Utils {    public static String MD5(String sourceStr) {        String result = "";        try {            MessageDigest md = MessageDigest.getInstance("MD5");            md.update(sourceStr.getBytes());            byte b[] = md.digest();            int i;            StringBuffer buf = new StringBuffer("");            for (int offset = 0; offset < b.length; offset++) {                i = b[offset];                if (i < 0)                    i += 256;                if (i < 16)                    buf.append("0");                buf.append(Integer.toHexString(i));            }            result = buf.toString();        } catch (NoSuchAlgorithmException e) {            System.out.println(e);        }        return result;    }}

SharedPreferences

import android.content.Context;import android.content.SharedPreferences;/** * SharePreference封装 *  *  */public class PrefUtils {    public static boolean getBoolean(Context ctx, String key, boolean defValue) {        SharedPreferences sp = ctx.getSharedPreferences("config",                Context.MODE_PRIVATE);        return sp.getBoolean(key, defValue);    }    public static void setBoolean(Context ctx, String key, boolean value) {        SharedPreferences sp = ctx.getSharedPreferences("config",                Context.MODE_PRIVATE);        sp.edit().putBoolean(key, value).commit();    }    public static void setString(Context ctx, String key, String value) {        SharedPreferences sp = ctx.getSharedPreferences("config",                Context.MODE_PRIVATE);        sp.edit().putString(key, value).commit();    }    public static String getString(Context ctx, String key, String defValue) {        SharedPreferences sp = ctx.getSharedPreferences("config",                Context.MODE_PRIVATE);        return sp.getString(key, defValue);    }    public static void setInt(Context ctx, String key, int value) {        SharedPreferences sp = ctx.getSharedPreferences("config",                Context.MODE_PRIVATE);        sp.edit().putInt(key, value).commit();    }    public static int getInt(Context ctx, String key, int defValue) {        SharedPreferences sp = ctx.getSharedPreferences("config",                Context.MODE_PRIVATE);        return sp.getInt(key, defValue);    }}
0 0
原创粉丝点击