国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android開發(fā)中4個(gè)常用的工具類

2020-02-21 17:21:56
字體:
供稿:網(wǎng)友

Android開發(fā)中,我們會(huì)使用許多相同的方法,我們經(jīng)常將這些方法匯總在一起,形成一個(gè)工具類,武林技術(shù)頻道小編會(huì)為大家提供全面、專業(yè)的Android開發(fā)中4個(gè)常用的工具類。

1、土司工具類(Toast管理)

/** * Toast統(tǒng)一管理類 * * @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;  /**   * 短時(shí)間顯示Toast   *   * @param context   * @param message   */  public static void show(Context context, CharSequence message) {    if (isShow)      Toast.makeText(context, message, Toast.LENGTH_SHORT).show();  }  /**   * 短時(shí)間顯示Toast   *   * @param context   * @param message   */  public static void showShort(Context context, int message) {    if (isShow)      Toast.makeText(context, message, Toast.LENGTH_SHORT).show();  }  /**   * 長(zhǎng)時(shí)間顯示Toast   *   * @param context   * @param message   */  public static void showLong(Context context, CharSequence message) {    if (isShow)      Toast.makeText(context, message, Toast.LENGTH_LONG).show();  }  /**   * 長(zhǎng)時(shí)間顯示Toast   *   * @param context   * @param message   */  public static void showLong(Context context, int message) {    if (isShow)      Toast.makeText(context, message, Toast.LENGTH_LONG).show();  }  /**   * 自定義顯示Toast時(shí)間   *   * @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時(shí)間   *   * @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 {  /**   * 保存在手機(jī)里面的文件名   */  public static final String FILE_NAME = "share_data";  /**   * 保存數(shù)據(jù)的方法,我們需要拿到保存數(shù)據(jù)的具體類型,然后根據(jù)類型調(diào)用不同的保存方法   *   * @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);  }  /**   * 得到保存數(shù)據(jù)的方法,我們根據(jù)默認(rèn)值得到保存的數(shù)據(jù)的具體類型,然后調(diào)用相對(duì)于的方法獲取值   *   * @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;  }  /**   * 移除某個(gè)key值已經(jīng)對(duì)應(yīng)的值   *   * @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);  }  /**   * 清除所有數(shù)據(jù)   *   * @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);  }  /**   * 查詢某個(gè)key是否已經(jīng)存在   *   * @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);  }  /**   * 返回所有的鍵值對(duì)   *   * @param context   * @return   */  public static Map<String, ?> getAll(Context context) {    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);    return sp.getAll();  }  /**   * 創(chuàng)建一個(gè)解決SharedPreferencesCompat.apply方法的一個(gè)兼容類   *   * @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執(zhí)行,否則使用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、網(wǎng)絡(luò)工具類

/** * 跟網(wǎng)絡(luò)相關(guān)的工具類 * @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");  }  /**   * 判斷網(wǎng)絡(luò)是否連接   *   * @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;  }  /**   * 打開網(wǎng)絡(luò)設(shè)置界面   */  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、獲得屏幕相關(guān)的輔助類

/** * 獲得屏幕相關(guān)的輔助類 * @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;  }  /**   * 獲得狀態(tài)欄的高度   *   * @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;  }  /**   * 獲取當(dāng)前屏幕截圖,包含狀態(tài)欄   *   * @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;  }  /**   * 獲取當(dāng)前屏幕截圖,不包含狀態(tài)欄   *   * @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;  }}

以上就是武林技術(shù)頻道小編介紹的關(guān)于Android開發(fā)中4個(gè)常用的工具類,如果你還想了解更多技術(shù)知識(shí),歡迎你隨時(shí)來js.Vevb.com學(xué)習(xí)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 民勤县| 建阳市| 东丽区| 和静县| 赤峰市| 濮阳县| 玉门市| 乌鲁木齐县| 芜湖市| 宾阳县| 定边县| 仪征市| 牟定县| 镇雄县| 石阡县| 恩施市| 含山县| 志丹县| 托克逊县| 樟树市| 星座| 岳阳市| 乾安县| 长顺县| 宿迁市| 湖南省| 包头市| 田东县| 桐梓县| 陇西县| 永康市| 海口市| 利辛县| 盐山县| 塘沽区| 池州市| 池州市| 虎林市| 舟曲县| 南宫市| 绥棱县|