Android开发中4个常用的工具类【Toast、SharedPreferences、网络及屏幕操作】
程序员文章站
2023-10-19 08:22:40
本文实例讲述了android开发中4个常用的工具类。分享给大家供大家参考,具体如下:
1、土司工具类(toast管理)
/**
* toast统一管理类...
本文实例讲述了android开发中4个常用的工具类。分享给大家供大家参考,具体如下:
1、土司工具类(toast管理)
/** * toast统一管理类 * * @project app_zxing * @package com.android.scan * @author chenlin * @version 1.0 * @date 2013年7月6日 * @note todo */ public class toastutil { private toastutil() { /* cannot be instantiated */ throw new unsupportedoperationexception("cannot be instantiated"); } public static boolean isshow = true; /** * 短时间显示toast * * @param context * @param message */ public static void show(context context, charsequence message) { if (isshow) toast.maketext(context, message, toast.length_short).show(); } /** * 短时间显示toast * * @param context * @param message */ public static void showshort(context context, int message) { if (isshow) toast.maketext(context, message, toast.length_short).show(); } /** * 长时间显示toast * * @param context * @param message */ public static void showlong(context context, charsequence message) { if (isshow) toast.maketext(context, message, toast.length_long).show(); } /** * 长时间显示toast * * @param context * @param message */ public static void showlong(context context, int message) { if (isshow) toast.maketext(context, message, toast.length_long).show(); } /** * 自定义显示toast时间 * * @param context * @param message * @param duration */ public static void show(context context, charsequence message, int duration) { if (isshow) toast.maketext(context, message, duration).show(); } /** * 自定义显示toast时间 * * @param context * @param message * @param duration */ public static void show(context context, int message, int duration) { if (isshow) toast.maketext(context, message, duration).show(); } }
2、sharedpreferences工具类
/** * sharedpreferences封装类sputils * @project app_zxing * @package com.android.scan * @author chenlin * @version 1.0 * @date 2013年6月6日 * @note todo */ public class sputils { /** * 保存在手机里面的文件名 */ public static final string file_name = "share_data"; /** * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法 * * @param context * @param key * @param object */ public static void put(context context, string key, object object) { sharedpreferences sp = context.getsharedpreferences(file_name, context.mode_private); sharedpreferences.editor editor = sp.edit(); if (object instanceof string) { editor.putstring(key, (string) object); } else if (object instanceof integer) { editor.putint(key, (integer) object); } else if (object instanceof boolean) { editor.putboolean(key, (boolean) object); } else if (object instanceof float) { editor.putfloat(key, (float) object); } else if (object instanceof long) { editor.putlong(key, (long) object); } else { editor.putstring(key, object.tostring()); } sharedpreferencescompat.apply(editor); } /** * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值 * * @param context * @param key * @param defaultobject * @return */ public static object get(context context, string key, object defaultobject) { sharedpreferences sp = context.getsharedpreferences(file_name, context.mode_private); if (defaultobject instanceof string) { return sp.getstring(key, (string) defaultobject); } else if (defaultobject instanceof integer) { return sp.getint(key, (integer) defaultobject); } else if (defaultobject instanceof boolean) { return sp.getboolean(key, (boolean) defaultobject); } else if (defaultobject instanceof float) { return sp.getfloat(key, (float) defaultobject); } else if (defaultobject instanceof long) { return sp.getlong(key, (long) defaultobject); } return null; } /** * 移除某个key值已经对应的值 * * @param context * @param key */ public static void remove(context context, string key) { sharedpreferences sp = context.getsharedpreferences(file_name, context.mode_private); sharedpreferences.editor editor = sp.edit(); editor.remove(key); sharedpreferencescompat.apply(editor); } /** * 清除所有数据 * * @param context */ public static void clear(context context) { sharedpreferences sp = context.getsharedpreferences(file_name, context.mode_private); sharedpreferences.editor editor = sp.edit(); editor.clear(); sharedpreferencescompat.apply(editor); } /** * 查询某个key是否已经存在 * * @param context * @param key * @return */ public static boolean contains(context context, string key) { sharedpreferences sp = context.getsharedpreferences(file_name, context.mode_private); return sp.contains(key); } /** * 返回所有的键值对 * * @param context * @return */ public static map<string, ?> getall(context context) { sharedpreferences sp = context.getsharedpreferences(file_name, context.mode_private); return sp.getall(); } /** * 创建一个解决sharedpreferencescompat.apply方法的一个兼容类 * * @author zhy * */ private static class sharedpreferencescompat { private static final method sapplymethod = findapplymethod(); /** * 反射查找apply的方法 * * @return */ @suppresswarnings({ "unchecked", "rawtypes" }) private static method findapplymethod() { try { class clz = sharedpreferences.editor.class; return clz.getmethod("apply"); } catch (nosuchmethodexception e) { } return null; } /** * 如果找到则使用apply执行,否则使用commit * * @param editor */ public static void apply(sharedpreferences.editor editor) { try { if (sapplymethod != null) { sapplymethod.invoke(editor); return; } } catch (illegalargumentexception e) { } catch (illegalaccessexception e) { } catch (invocationtargetexception e) { } editor.commit(); } } }
3、网络工具类
/** * 跟网络相关的工具类 * @project app_zxing * @package com.android.scan * @author chenlin * @version 1.0 * @date 2013年6月8日 * @note todo */ public class netutils { private netutils() { /* cannot be instantiated */ throw new unsupportedoperationexception("cannot be instantiated"); } /** * 判断网络是否连接 * * @param context * @return */ public static boolean isconnected(context context) { connectivitymanager connectivity = (connectivitymanager) context.getsystemservice(context.connectivity_service); if (null != connectivity) { networkinfo info = connectivity.getactivenetworkinfo(); if (null != info && info.isconnected()) { if (info.getstate() == networkinfo.state.connected) { return true; } } } return false; } /** * 判断是否是wifi连接 */ public static boolean iswifi(context context) { connectivitymanager cm = (connectivitymanager) context.getsystemservice(context.connectivity_service); if (cm == null) return false; return cm.getactivenetworkinfo().gettype() == connectivitymanager.type_wifi; } /** * 打开网络设置界面 */ public static void opensetting(activity activity) { intent intent = new intent("/"); componentname cm = new componentname("com.android.settings", "com.android.settings.wirelesssettings"); intent.setcomponent(cm); intent.setaction("android.intent.action.view"); activity.startactivityforresult(intent, 0); } }
4、获得屏幕相关的辅助类
/** * 获得屏幕相关的辅助类 * @project app_zxing * @package com.android.scan * @author chenlin * @version 1.0 * @date 2013年6月6日 */ public class screenutils { private screenutils() { /* cannot be instantiated */ throw new unsupportedoperationexception("cannot be instantiated"); } /** * 获得屏幕高度 * * @param context * @return */ public static int getscreenwidth(context context) { windowmanager wm = (windowmanager) context.getsystemservice(context.window_service); displaymetrics outmetrics = new displaymetrics(); wm.getdefaultdisplay().getmetrics(outmetrics); return outmetrics.widthpixels; } /** * 获得屏幕宽度 * * @param context * @return */ public static int getscreenheight(context context) { windowmanager wm = (windowmanager) context.getsystemservice(context.window_service); displaymetrics outmetrics = new displaymetrics(); wm.getdefaultdisplay().getmetrics(outmetrics); return outmetrics.heightpixels; } /** * 获得状态栏的高度 * * @param context * @return */ public static int getstatusheight(context context) { int statusheight = -1; try { class<?> clazz = class.forname("com.android.internal.r$dimen"); object object = clazz.newinstance(); int height = integer.parseint(clazz.getfield("status_bar_height").get(object).tostring()); statusheight = context.getresources().getdimensionpixelsize(height); } catch (exception e) { e.printstacktrace(); } return statusheight; } /** * 获取当前屏幕截图,包含状态栏 * * @param activity * @return */ public static bitmap snapshotwithstatusbar(activity activity) { view view = activity.getwindow().getdecorview(); view.setdrawingcacheenabled(true); view.builddrawingcache(); bitmap bmp = view.getdrawingcache(); int width = getscreenwidth(activity); int height = getscreenheight(activity); bitmap bp = null; bp = bitmap.createbitmap(bmp, 0, 0, width, height); view.destroydrawingcache(); return bp; } /** * 获取当前屏幕截图,不包含状态栏 * * @param activity * @return */ public static bitmap snapshotwithoutstatusbar(activity activity) { view view = activity.getwindow().getdecorview(); view.setdrawingcacheenabled(true); view.builddrawingcache(); bitmap bmp = view.getdrawingcache(); rect frame = new rect(); activity.getwindow().getdecorview().getwindowvisibledisplayframe(frame); int statusbarheight = frame.top; int width = getscreenwidth(activity); int height = getscreenheight(activity); bitmap bp = null; bp = bitmap.createbitmap(bmp, 0, statusbarheight, width, height - statusbarheight); view.destroydrawingcache(); return bp; } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android资源操作技巧汇总》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。