关于android开发通用的工具类篇《一》

       哈喽,这里是本人在开发中经常使用的开发工具类,贴代码就可以直接使用,嘎嘎方便,带给大家,希望能帮助到各位笔友。 

1,日志工具类——LogUtils(常用)

public class LogUtils {

    public static boolean isShowLog = true; //一般在写项目的时候往往的打开日志,但是在发布版本时为了节省log产生的信息

    public static void LogI(String Tag, String msg){
        if(isShowLog && msg != null){
            Log.i(Tag, msg);
        }
    }

    public static void LogE(String Tag , String msg){
        if(isShowLog && msg != null){
            Log.e(Tag,msg);
        }
    }

    public static void LogD(String msg){
        if(isShowLog && msg != null){
            Log.d(Tag, msg);
        }
    }

    public static void LogV(String msg){
        if(isShowLog && msg != null){
            Log.v(Tag, msg);
        }
    }
}

2,toast工具类——ToastUtils

public class ToastUtils {

    //展示时长的长短可以自己设置,改变一下LENGTH_SHORT就好了

    public static void toastBottom( String body){
        Toast.makeText(BaseApplication.mContext, body , Toast.LENGTH_SHORT).show();
    }

    public static void taostBottomLong( String body){
        Toast.makeText(BaseApplication.mContext , body , Toast.LENGTH_SHORT).show();
    }

    public static void taostCenter(String body){
        Toast toast = new Toast(BaseApplication.mContext);
        toast.setGravity(Gravity.CENTER , 0 , 0);
        toast.setText(body);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

    public static void taostCenterLong( String body){
        Toast toast = new Toast(BaseApplication.mContext);
        toast.setGravity(Gravity.CENTER , 0 , 0);
        toast.setText(body);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

}

 3,保留数据类——SpUtils(常用)

public class SpUtils {

    //把自己需要保留的变量名定义一下就可以
    private static SpUtils mInstance;
    private SharedPreferences mSharedPreferences;
    private final String SHARE_NAME = "包名";

    public static final String USERID = "id" ;
    public static final String TIME = "time" ;
    public static final String MACADDRESS="macaddress";
    public static final String TOKEN = "token";
    public static final String SN = "sn";

    private SpUtils(){
        mSharedPreferences = App.mContext.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
    }

    public static SpUtils getInstance(){
        if (mInstance == null){
            synchronized (SpUtils.class){
                if (mInstance == null){
                    mInstance = new SpUtils();
                }
            }
        }
        return mInstance;
    };

    /**
     * 保存一个整形
     */
    public void setIntValue(String key, int value){
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key,value);
        editor.commit();
    }

    /**
     * 获取一个整形
     */
    public int getIntValue(String key){
        return mSharedPreferences.getInt(key,0);
    }

    /**
     * 保存一个字符串
     */
    public void setStringValue(String key, String value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    /**
     * 获取一个字符串
     */
    public String getStringValue(String key) {
        return mSharedPreferences.getString(key, "");
    }

    /**
     * 保存一个long
     */
    public void setLongValue(String key, long value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    /**
     * 获取一个long
     */
    public Long getLongValue(String key) {
        return mSharedPreferences.getLong(key,0L);
    }

    /**
     * 保存一个boolean
     */
    public void setBooleanValue(String key, boolean value){
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    /**
     * 获取一个boolean
     */
    public boolean getBooleanValue(String key){
        return mSharedPreferences.getBoolean(key,false);
    }
}

使用:

//保存数据
SpUtils.getInstance().setStringValue(SpUtils.变量名,值);

//取值
SpUtils.getInstance().setStringValue(SpUtils.变量名,值);

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HONikn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值