實用工具類

第一個:SharedPreferences:

在使用這個工具類的時候我遇到兩個坑:
第一個坑、有一個getValue的方法中用到了下面的代碼導致將數據清除,導致我存儲的數據找不到,花了很長時間才解決。so,如過我們希望置空某個函數的話,正確方法應該是remove:


SharedPreferences.Editor sp = getEditor(context, name);
        //錯誤置空
         sp.clear();
         //正確的方法
         //sp.remove(key);
       ...

第二個坑、下面的代碼還可以繼續根據自己的需要進行擴展,比如我擴展了ArrayList< Long> getLongData(Context context,String name,String key,boolean empty)方法,其實是 ArrayList getDataList(Context context, String name, String key, Type type, boolean empty)得到的。爲什麼沒有直接使用上面的方法來獲取呢?代碼邏輯是沒有問題,編譯的時候也不會報錯。但是我在時機運行的時候發現數據會將我們存儲的Long類型數據取出來,默認轉爲Double類型,因此我們獲取Long類型的數據時會報錯,類型轉換異常。我仔細檢查了自己的代碼沒有問題,面對着莫名其妙的異常通過斷點才發現是系統自身給我轉換了的原因。這裏沒有找到爲什麼會默認爲Double的原因(遇到這種問題實在沒有時間來琢磨,只要可以解決就好。),但是還是通過代碼解決了,在使用的時候可以繼續擴展,但是要注意這個坑!!



import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * SharedPreferences 工具類
 * Created by 魏興 on 2017/5/18.
 */

public class SharedPrefsUtil {
    /**
     * 向SharedPreferences中寫入int類型數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param value 值
     */
    public static void putValue(Context context, String name, String key,
                                int value) {
        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putInt(key, value);
        sp.commit();
    }

    /**
     * 向SharedPreferences中寫入boolean類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param value 值
     */
    public static void putValue(Context context, String name, String key,
                                boolean value) {
        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putBoolean(key, value);
        sp.commit();
    }

    /**
     * 向SharedPreferences中寫入String類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param value 值
     */
    public static void putValue(Context context, String name, String key,
                                String value) {
        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putString(key, value);
        sp.commit();
    }

    /**
     * 向SharedPreferences中寫入float類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param value 值
     */
    public static void putValue(Context context, String name, String key,
                                float value) {
        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putFloat(key, value);
        sp.commit();
    }

    /**
     * 向SharedPreferences中寫入long類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param value 值
     */
    public static void putValue(Context context, String name, String key,
                                long value) {

        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putLong(Const.PRIVIEW_CURRENT, value);
        sp.commit();

    }

    /**
     * 向SharedPreferences中寫入Double類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param value 值
     */
    public static void putValue(Context context, String name, String key,
                                Double value) {

        SharedPreferences.Editor sp = getEditor(context, name);
        sp.remove(key);
        Gson gson = new Gson();
        //轉換成json數據,再保存
        String strJson = gson.toJson(value);
        SharedPreferences.Editor editor = getEditor(context, name);
        editor.putString(key, strJson);
        editor.commit();}


        /**
         *  向SharedPreferences中寫入List類型的數據
         *
         * @param context 上下文環境
         * @param name 對應的xml文件名稱
         * @param key 值
         * @param datalist 值
         * @param <T> 數據類型
         */
    public static <T> void setDataList(Context context,String name,String key, List<T> datalist) {
        if (null == datalist || datalist.size() <= 0)
            return;

        Gson gson = new Gson();
        //轉換成json數據,再保存
        String strJson = gson.toJson(datalist);
        SharedPreferences.Editor editor = getEditor(context, name);
        editor.putString(key, strJson);
        editor.commit();

    }
    /**
     * 從SharedPreferences中讀取int類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param defValue 如果讀取不成功則使用默認值
     * @param empty 是否置空
     * @return  返回讀取的值
     */
    public static int getValue(Context context, String name, String key,
                               int defValue,boolean empty) {
        SharedPreferences sp = getSharedPreferences(context, name);
        int value = sp.getInt(key, defValue);
        if(empty){
            sp.edit().remove(key);
        }
        return value;
    }

    /**
     * 從SharedPreferences中讀取boolean類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param defValue 如果讀取不成功則使用默認值
     * @param empty 是否置空
     * @return  返回讀取的值
     */
    public static boolean getValue(Context context, String name, String key,
                                   boolean defValue,boolean empty) {
        SharedPreferences sp = getSharedPreferences(context, name);
        boolean value = sp.getBoolean(key, defValue);
        if(empty){
            sp.edit().remove(key);
        }
        return value;
    }

    /**
     * 從SharedPreferences中讀取String類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param defValue 如果讀取不成功則使用默認值
     * @param empty 是否置空
     * @return  返回讀取的值
     */
    public static String getValue(Context context, String name, String key,
                                  String defValue,boolean empty) {
        SharedPreferences sp = getSharedPreferences(context, name);
        String value = sp.getString(key, defValue);
        if(empty)
            sp.edit().remove(key);
        return value;
    }

    /**
     * 從SharedPreferences中讀取float類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param defValue 如果讀取不成功則使用默認值
     * @param empty 是否置空
     * @return  返回讀取的值
     */
    public static float getValue(Context context, String name, String key,
                                 float defValue,boolean empty) {
        SharedPreferences sp = getSharedPreferences(context, name);
        float value = sp.getFloat(key, defValue);
        if(empty)
            sp.edit().remove(key);
        return value;
    }

    /**
     * 從SharedPreferences中讀取long類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param defValue 如果讀取不成功則使用默認值
     * @return  返回讀取的值
     */
    public static long getValue(Context context, String name, String key,
                                long defValue,boolean empty) {

        SharedPreferences sp = getSharedPreferences(context, name);
        long value = sp.getLong(Const.PRIVIEW_CURRENT, defValue);
        if(empty)
            sp.edit().remove(key);

        return value;
    }
    /**
     * 從SharedPreferences中讀取long類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param defValue 如果讀取不成功則使用默認值
     * @return  返回讀取的值
     */
    public static Double getValue(Context context, String name, String key,
                                double defValue,boolean empty) {

        SharedPreferences sp = getSharedPreferences(context, name);
        String value = sp.getString(key,null);
        if(null==value){
            return defValue;
        }
        Gson gson = new Gson();
        Double result = gson.fromJson(value,Double.class);
        if(empty)
            sp.edit().remove(key);
        return result;
    }

    /**
     * 從SharedPreferences中讀取List類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param empty 是否置空
     * @param <T> 數據類型
     * @return 返回List數據
     */
    public static <T> ArrayList<T> getDataList(Context context, String name, String key, Type type, boolean empty) {

        ArrayList<T> datalist=new ArrayList<T>();
        SharedPreferences sp = getSharedPreferences(context, name);
        String strJson = sp.getString(key, null);
        if (null == strJson) {
            return datalist;
        }
        Gson gson = new Gson();
        datalist = gson.fromJson(strJson,type);
        if(empty)
            sp.edit().remove(key);
        return datalist;

    }

    /**
     * 從SharedPreferences中讀取ArrayList<Long> 類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param empty 是否置空
     * @return 返回ArrayList<Long>
     */
    public static ArrayList<Long> getLongData(Context context,String name,String key,boolean empty){
        return getDataList(context,name,key,new TypeToken<List<Long>>() {
        }.getType(),empty);
    }
    /**
     * 從SharedPreferences中讀取ArrayList<String> 類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param empty 是否置空
     * @return 返回ArrayList<Long>
     */
    public static ArrayList<String> getStringData(Context context,String name,String key,boolean empty){
        return getDataList(context,name,key,new TypeToken<List<String>>() {
        }.getType(),empty);
    }

    /**
     * 從SharedPreferences中讀取ArrayList<Integer> 類型的數據
     *
     * @param context 上下文環境
     * @param name 對應的xml文件名稱
     * @param key 鍵
     * @param empty 是否置空
     * @return 返回ArrayList<Long>
     */
    public static ArrayList<Integer> getIntegerData(Context context,String name,String key,boolean empty){
        return getDataList(context,name,key,new TypeToken<List<Integer>>() {
        }.getType(),empty);
    }

    //獲取Editor實例
    private static SharedPreferences.Editor getEditor(Context context, String name) {
        return getSharedPreferences(context, name).edit();
    }

    //獲取SharedPreferences實例
    private static SharedPreferences getSharedPreferences(Context context, String name) {
        return context.getApplicationContext().getSharedPreferences(name, Context.MODE_PRIVATE);
    }
}

第二個:日誌管理類:

(申明:這個類是用了鴻神張鴻洋的類,然後自己簡單加工了一下。)


/**
 * 日誌工具類
 * Created by 魏興 on 2017/5/12.
 */

public class LUtil {
    public static boolean isDebug = true;// 是否需要打印bug,可以在application的onCreate函數裏面初始化
    private static final String TAG = "ugly";

    // 下面四個是默認tag的函數
    public static void i(String msg)
    {
        if (isDebug){
            if(null==msg)msg="";
            Log.i(TAG, msg);
        }

    }

    public static void d(String msg)
    {
        if (isDebug){
            if(null==msg)msg="";
            Log.d(TAG, msg);
        }

    }

    public static void e(String msg)
    {
        if (isDebug){
            if(null==msg)msg="";
            Log.e(TAG, msg);
        }

    }
    public static void e(String msg,Exception e)
    {
        if (isDebug){
            if(null==msg)msg="";
            if(null!=e)
            Log.e(TAG, msg,e);
            else  Log.e(TAG, msg);
        }

    }

    public static void v(String msg)
    {
        if (isDebug){
            if(null==msg)msg="";
            Log.v(TAG, msg);
        }

    }

    // 下面是傳入自定義tag的函數
    public static void i(String tag, String msg)
    {
        if (isDebug){
            if(null==msg)msg="";
            Log.i(tag, msg);
        }

    }

    public static void d(String tag, String msg)
    {
        if (isDebug){
            if(null==msg)msg="";
            Log.i(tag, msg);
        }

    }

    public static void e(String tag, String msg)
    {
        if (isDebug){
            if(null==msg)msg="";
            Log.i(tag, msg);
        }

    }
    public static void e(String tag, String msg,Exception e)
    {
        if (isDebug){
            if(null==msg)msg="";
            Log.i(tag, msg);
        }

    }
    public static void v(String tag, String msg)
    {
        if (isDebug){
            if(null==msg)msg="";
            Log.i(tag, msg);
        }

    }
}

第三個:小米手機權限檢查:

(新鮮的方法,複製過來還沒有封裝,自己看看怎麼用再封裝!)

/**
     * 檢查小米的權限
     * @return
     */
    private boolean checkXiaoMi() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            Log.w(TAG, "hasPermissions: API version < M, returning true by default");
            return true;
        }
        int autn = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
        AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
        int checkOp = appOpsManager.checkOp(AppOpsManager.OPSTR_FINE_LOCATION, Process.myUid(), getPackageName());
        if (autn== PackageManager.PERMISSION_GRANTED&&checkOp== AppOpsManager.MODE_ALLOWED) {
            return true;
        }
        if (autn== PackageManager.PERMISSION_GRANTED&&checkOp== AppOpsManager.MODE_IGNORED) {
            Toast.makeText(this, "沒有權限", Toast.LENGTH_SHORT).show();
            return false;
        }
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
        return false;
    }
發佈了61 篇原創文章 · 獲贊 28 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章