Android開發(fā)中一些常用的工具方法

1. 獲取debug狀態(tài)

/**
* 檢測是否處于Debug模式
*
* @return boolean
*/
 public static boolean isDebugMode(Context context) {
    ApplicationInfo info =    context.getApplicationInfo();
                if (info != null && (info.flags &    ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
          return true;
    }
    return false;
    }

2. 得到當前版本信息

  /**
   * 獲取軟件版本號
   */
  public static String getVersonName(Context context) {
    String versionName = null;
    PackageManager pm = context.getPackageManager();
    PackageInfo info = null;

    try {
      info = pm.getPackageInfo(context.getApplicationContext().getPackageName(), 0);
    } catch (NameNotFoundException e) {
      e.printStackTrace();
    }

    if (info != null) {
      versionName = info.versionName;
    }
    return versionName;
  }

3. 得到系統(tǒng)名

 public static String getOsName() {
     String MANUFACTURER = Build.MANUFACTURER;
     return MANUFACTURER;
  }

4. 判斷當前字符是數字

public static boolean isNumeric(String str) {
    if (TextUtils.isEmpty(str)) {
      return false;
    }
    Pattern pattern = Pattern.compile("[0-9]*");
    Matcher isNum = pattern.matcher(str);
    return isNum.matches();
    }

5. 驗證手機號碼

/**
   * 驗證手機格式
   */
  public static boolean isMobileNumber(String mobileNumber) {
    /*
     * 移動:134、135、136、137、138、139、150、151、157(TD)、140(TD)、158、159、187、188
     * 聯(lián)通:130、131、132、152、155、156、185、186 電信:133、153、180、189、(1349衛(wèi)通)
     * 總結起來就是第一位必定為1,第二位必定為3或5或8,其他位置的可以為0-9 虛擬運營商:170
     */
    // String regex = "[1][34578]\\d{9}";//
    // "[1]"代表第1位為數字1,"[358]"代表第二位可以為3、5、8中的一個,"\\d{9}"代表后面是可以是0~9的數字,有9位。
    String regex = "[1]\\d{10}";
    if (TextUtils.isEmpty(mobileNumber)) {
      return false;
    } else {
      return mobileNumber.matches(regex);
    }
  }

6. Bitmap顏色轉換

 public static Bitmap translateBitmap(Bitmap bm, int r, int g, int b) {

    if (bm == null)
      return null;

    try {
      int w = bm.getWidth();
      int h = bm.getHeight();
      int size = w * h;
      int[] array = new int[size];
      bm.getPixels(array, 0, w, 0, 0, w, h);

      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (array[i * w + j] != 0) {
            int color = array[i * w + j];

            int alpha = ((color & 0xff000000) >> 24);
            int red = r;
            int green = g;
            int blue = b;

            int pix = (alpha << 24) | (red << 16) | (green << 8) | blue;
            array[i * w + j] = pix;

          }
        }

      }
      if (bm.isMutable()) //此情況可以直接改變像素點
      {
        try {
          bm.setPixels(array, 0, w, 0, 0, w, h);
        } catch (Exception e) {I
          //設置像素點出錯
        }
      } else {
        bm = Bitmap.createBitmap(array, w, h, Config.ARGB_8888);
      }
      return bm;
    } catch (OutOfMemoryError e) {
      e.printStackTrace();
    }

    return bm;
  }

7. 通過View生成Bitmap

  /**
   * Returns a bitmap showing a screenshot of the view passed in.
   */
  private Bitmap getBitmapFromView(View v) {
    Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    v.draw(canvas);
    return bitmap;
  }

8. dp,px 轉換

  /**
     * Converts an unpacked complex data value holding a dimension to its final floating 
     * point value. The two parameters <var>unit</var> and <var>value</var>
     * are as in {@link #TYPE_DIMENSION}.
     *  
     * @param unit The unit to convert from.
     * @param value The value to apply the unit to.
     * @param metrics Current display metrics to use in the conversion -- 
     *                supplies display density and scaling information.
     * 
     * @return The complex floating point value multiplied by the appropriate 
     * metrics depending on its unit. 
     */
    public static float applyDimension(int unit, float value,
                                       DisplayMetrics metrics)
    {
        switch (unit) {
        case COMPLEX_UNIT_PX:
            return value;
        case COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f/72);
        case COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f/25.4f);
        }
        return 0;
    }
這個方法在TypedValue中

9. 判斷網絡狀態(tài)是否可以用

 public static boolean isNetWorkAvailable(Context mContext) {
    ConnectivityManager mConnectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nInfo = mConnectivity.getActiveNetworkInfo();
    if (nInfo == null) {
      return false;
    } else {
      return nInfo.isConnected();
    }


  }

10.得到當前設備名,例如,vivoX3,HUAWEIP7-L10

  private String getModel() {

    String model = android.os.Build.MODEL;

    if (model != null) {

      model = model.trim().replaceAll("\\s*", "");
    } else {

      model = "";
    }

    return model;
  }

11. 獲取AndroidManifest.xmlmeta中的數據

   PackageManager packageManager = mContext.getPackageManager();
   String packageName = mContext.getPackageName();
   ApplicationInfo applicationInfo=packageManager
   .getApplicationInfo(packageName,PackageManager.GET_META_DATA);
   
   applicationInfo.metaData.getXX(x,x);

12. 打開Apk文件進行安裝

    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file),
        "application/vnd.android.package-archive");
    context.startActivity(intent);

13.打開應用市場

     Uri uri = Uri.parse("market://details?id=" + this.getPackageName());
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     try {
          //可能手機上沒有安裝應用市場
          startActivity(entry.jumpIntent);
         } catch (ActivityNotFoundException e) {
           
         }

14.調用系統(tǒng)方式分享文件

 public static void videoShare(Activity activity, String videoPath, int requestcode) {

    if (TextUtils.isEmpty(videoPath)) {
      return;
    }

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("video/*");  //image/*
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(videoPath)));

    activity.startActivityForResult(Intent.createChooser(intent, activity.getString(R.string.share_text)), requestcode);
  }

更多分享可以查看官方介紹:

Intent 分享源碼介紹
Intent 分享源碼介紹

后續(xù)過程中再補充

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容