Android开发10个常用工具类
原文出处 张鸿洋的博客
打开大家手上的项目基本都会有一大批的辅助类今天特此整理出10个基本每个项目中都会使用的工具类用于快速开发~~
在此感谢群里给我发项目中工具类的兄弟/姐妹~
1、 日志工具类L.javap ac kage c om.zhy.utils;import android.util.Log;
*Lo g统一管理类public class Lprivate L()
/*cannot be instantiated*/throw new UnsupportedOperationExc eption(c annot be instantiatedpublic static boolean isDebug=true;//是否需要打印bug可以在application的onCreate函数里面初始化private static final String TAG=way
//下面四个是默认tag的函数public static void i(String msg)if(is Debug)
Lo g.i(TAG,m s g);public static void d(String msg)if(is Debug)
Lo g.d(TAG,ms g);public static void e(String msg)if(is Debug)
Lo g.e(TAG,m s g);public static void v(String msg)if(is Debug)
Lo g.v(TAG,ms g);
//下面是传入自定义tag的函数public static void i(String tag,String msg)if(is Debug)
Log.i(tag,ms g);public static void d(String tag,String msg)if(is Debug)
Log.i(tag,ms g);public static void e(String tag,String msg)if(is Debug)
Log.i(tag,ms g);public static void v(String tag,String msg)if(is Debug)
Log.i(tag,ms g);
}
网上看到的类注释上应该原创作者的名字很简单的一个类 网上也有很多提供把日
志记录到SDCard上的不过我是从来没记录过所以引入个最简单的大家可以进行评价是否需要扩充~~
2、 To ast统一管理类p ac kage c om.zhy.utils;import android.c ontent.Context;import android.widget.Toast;
*To ast统一管理类public classTprivate T()
/**cannot be instantiated**/throw new UnsupportedOperationExc eption(c annot be instantiatedpublic static boolean isShow=true;
/**
*短时间显示To as t
*@param context
*@param messagepublic static void showShort(Context context,CharSequence message)if(is Show)
Toast.makeText(c ontext,mes sage,Toast.LENGTH_SHORT).show();
/**
*短时间显示To as t
*@param context
*@param messagepublic static void showShort(Context context, int message)if(is Show)
Toast.makeText(c ontext,mes sage,Toast.LENGTH_SHORT).show();/**
*长时间显示To as t
*@param context
*@param messagepublic static void showLong(Context context,CharSequence message)if(is Show)
Toast.makeText(c ontext,mes s age,Toast.LENGTH_LONG).show();/**
*长时间显示To as t
*@param context
*@param messagepublic static void showLong(Context context, int message)if(is Show)
Toast.makeText(c ontext,mes s age,Toast.LENGTH_LONG).show();/**
* 自定义显示To as t时间
*@param context
*@param message
*@param durationpublic static void show(Context context,CharSequence message, int duration)if(is Show)
Toast.makeText(c ontext,mes s age,duration).show();
/**
* 自定义显示To as t时间
*@param context
*@param message
*@param durationpublic static void show(Context context, int message, int duration)if(is Show)
Toast.makeText(c ontext,mes s age,duration).show();
}
也是非常简单的一个封装能省则省了~~
3、 SharedPreferenc es封装类SPUtilsp ac kage c om.zhy.utils;import java.lang.reflect.Invoc ationTargetExc eption;import java.lang.reflec t.Method;import java.util.Map;import android.c ontent.Context;import android.c ontent.SharedPreferenc es;public class SPUtils
/**
*保存在手机里面的文件名public static final String FILE_NAME=share_data
/**
*保存数据的方法我们需要拿到保存数据的具体类型然后根据类型调用不同的保存方法
*@param context
*@param key
*@param objectpublic static void put(Context c ontext,String key,Object object)
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,
Context.MODE_PRI VATE);
SharedPreferenc es.Editor editor=sp.edit();if(object instanceof String)editor.putString(key, (String)objec t);
} else if(object instanceof Integer)editor.putInt(key, (Integer)objec t);
} else if(object instanceof Boolean)editor.putBoo lean(key, (Boo le an)object);
} else if(object instanceof Float)editor.putFloat(key, (Float)objec t);
} else if(object instanceof Long)
editor.putLong(key, (Long)objec t);
} els eeditor.putString(key,object.toString());
SharedPreferenc es Compat.apply(editor);
/**
*得到保存数据的方法我们根据默认值得到保存的数据的具体类型然后调用相对于的方法获取值
*@param context
*@param key
*@param defaultObject
*@r eturnpublic static Object get(Context context,String key,Object defaultObject)
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,
Context.MODE_PRI VATE);if(defaultObject instanceof String)return sp.getString(key, (String)defaultObjec t);
} else if(defaultObject instanceof Integer)return sp.getInt(key, (Integer)defaultObj ec t);
} else if(defaultObject instanceof Boolean)return sp.getBoo lean(key, (Boo lean)defaultObj ect);
} else if(defaultObject instanceof Float)return sp.getFloat(key, (Float)defaultObjec t);
} else if(defaultObject instanceof Long)
return sp.getLong(key, (Long)defaultObjec t);r eturn null;
/**
*移除某个key值已经对应的值
*@param context
*@param keypublic static void remove(Context context,String key)
SharedPreferences sp=c ontext.getSharedPreferences(FILE_NAME,Context.MODE_PRI VATE);
SharedPreferenc es.Editor editor=sp.edit();editor.remove(key);
SharedPreferenc es Compat.apply(editor);
/**
*清除所有数据
*@param contextpublic static void clear(Context context)
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,Context.MODE_PRI VATE);
SharedPreferenc es.Editor editor=sp.edit();editor.clear();
SharedPreferenc es Compat.apply(editor);
/**
*查询某个key是否已经存在
*@param context
*@param key
*@r eturnpublic static boolean contains(Context context,String key)
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,Context.MODE_PRI VATE);return sp.c ontains(key);
/**
*返回所有的键值对
*@param context
*@r eturnpublic static Map String, ?getAll(Context context)
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,Context.MODE_PRI VATE);r eturn s p.g etAl l();
/**
*创建一个解决SharedPreferenc es Compat.apply方法的一个兼容类
*@author zhyprivate static class SharedPreferencesCompatprivate static final Method sApp lyMethod=findApp lyMethod();/**
今天早上相比很多网友和一样收到来自Linode的庆祝18周年的邮件信息。和往年一样,他们会回顾在过去一年中的成绩,以及在未来准备改进的地方。虽然目前Linode商家没有提供以前JP1优化线路的机房,但是人家一直跟随自己的脚步在走,确实在云服务器市场上有自己的立足之地。我们看看过去一年中Linode的成就:第一、承诺投入 100,000 美元来帮助具有社会意识的非营利组织,促进有价值的革新。第二、发...
轻云互联成立于2018年的国人商家,广州轻云互联网络科技有限公司旗下品牌,主要从事VPS、虚拟主机等云计算产品业务,适合建站、新手上车的值得选择,香港三网直连(电信CN2GIA联通移动CN2直连);美国圣何塞(回程三网CN2GIA)线路,所有产品均采用KVM虚拟技术架构,高效售后保障,稳定多年,高性能可用,网络优质,为您的业务保驾护航。官方网站:点击进入广州轻云网络科技有限公司活动规则:1.用户购...
Digital-VM商家的暑期活动促销,这个商家提供有多个数据中心独立服务器、VPS主机产品。最低配置月付80美元,支持带宽、流量和IP的自定义配置。Digital-VM,是2019年新成立的商家,主要从事日本东京、新加坡、美国洛杉矶、荷兰阿姆斯特丹、西班牙马德里、挪威奥斯陆、丹麦哥本哈根数据中心的KVM架构VPS产品销售,分为大硬盘型(1Gbps带宽端口、分配较大的硬盘)和大带宽型(10Gbps...