首页 > 系统 > Android > 正文

android常用的工具类

2019-11-09 17:36:44
字体:
来源:转载
供稿:网友

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.LogUtil
public 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);   }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表