实用工具类

第一个: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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章