1.設備相關的工具類DeviceUtil(獲取屏幕大小,狀態欄高度,鍵盤操作,版本號,dip與px轉化)
public class DeviceUtil{ /** * 獲得屏幕高度 * * @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; /** * 該方法用于將dip大小轉換為px大小 */ public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } /** * 該方法用于將px大小轉換為dip大小 */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 獲取當前屏幕截圖,不包含狀態欄 * @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; } /** * 打開鍵盤. * * @param context the context */ public static void showSoftInput(Context context) { InputMethodManager inputMethodManager = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } /** * 關閉鍵盤事件. * * @param context the context */ public static void closeSoftInput(Context context) { InputMethodManager inputMethodManager = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager != null && ((Activity) context).getCurrentFocus() != null) { inputMethodManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus() .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } /** * 獲取當前版本號 * @return * @throws Exception */ public static String getVersionName(Context context) { StringBuilder builder = new StringBuilder("v"); try { PackageInfo packInfo = getPackageInfo(context); builder.append(packInfo.versionName); } catch (Exception e) { e.printStackTrace(); } return builder.toString(); } /** * 是否存在該包名的應用 * @param context * @return */ public static boolean exitAppBy(Context context, String packageName) { final PackageManager packageManager = context.getPackageManager(); List<PackageInfo> packageInfos= packageManager.getInstalledPackages(0);// 獲取所有已安裝程序的包信息 if (packageInfos!= null) { for (int i = 0; i < pinfo.size(); i++) { String pn = packageInfos.get(i).packageName; if (pn.equals(packageName)) { return true; } } } return false; }2.LogUtilpublic class LogUtil { private static final boolean DEBUG = false; public static void i(String tag, String msg) { if (DEBUG) { Log.i(tag, msg); } } public static void e(String tag, String msg) { if (DEBUG) { Log.e(tag, msg); } } public static void d(String tag, String msg) { if (DEBUG) { Log.d(tag, msg); } } public static void w(String tag, String msg) { if (DEBUG) { Log.w(tag, msg); } } public static void v(String tag, String msg) { if (DEBUG) { Log.v(tag, msg); } }3.加密md5Util
public class MD5Util { public static String md5(String str) { if (str == null) { return null; } return md5(str, "utf-8"); } public static String md5(String str, String encodingType) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { e.printStackTrace(); return null; } try { md5.update(str.getBytes(encodingType)); } catch (UnsupportedEncodingException e) { md5.update(str.getBytes()); } byte[] md5Bytes = md5.digest(); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); }}4.偏好緩存SPUtil
public class SPUtil { private static final String name="sp_data"; public static SharedPreferences getDefaultSharedPreferences(Context context) { return context.getSharedPreferences(name, Context.MODE_PRIVATE); } public static void putInt(Context context,String key, int value) { SharedPreferences sharedPreferences = getDefaultSharedPreferences(context); SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putInt(key, value); edit.commit(); } public static int getInt(Context context,String key) { SharedPreferences sharedPreferences = getDefaultSharedPreferences(context); return sharedPreferences.getInt(key, 0); } public static void putString(Context context,String key, String value) { SharedPreferences sharedPreferences = getDefaultSharedPreferences(context); SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putString(key, value); edit.commit(); } public static String getString(Context context,String key) { SharedPreferences sharedPreferences = getDefaultSharedPreferences(context); return sharedPreferences.getString(key,null); } public static void putBoolean(Context context,String key, boolean value) { SharedPreferences sharedPreferences = getDefaultSharedPreferences(context); SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putBoolean(key, value); edit.commit(); } public static boolean getBoolean(Context context,String key,boolean defValue) { SharedPreferences sharedPreferences = getDefaultSharedPreferences(context); return sharedPreferences.getBoolean(key,defValue); }}
新聞熱點
疑難解答