哈喽,这里是本人在开发中经常使用的开发工具类,贴代码就可以直接使用,嘎嘎方便,带给大家,希望能帮助到各位笔友。
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.变量名,值);