public static Properties collectDeviceInfo(Context context) { Properties mDeviceCrashInfo = new Properties(); try { PackageManager pm = context.getPackageManager(); PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES); if (pi != null) { mDeviceCrashInfo.put(VERSION_NAME, pi.versionName == null ? "not set" : pi.versionName); mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode); } } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, "Error while collect package info", e); } Field[] fields = Build.class.getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); mDeviceCrashInfo.put(field.getName(), field.get(null)); } catch (Exception e) { Log.e(TAG, "Error while collect crash info", e); } } return mDeviceCrashInfo; } public staticStringcollectDeviceInfoStr(Context context) { Properties prop = collectDeviceInfo(context); Set deviceInfos = prop.keySet(); StringBuilder deviceInfoStr = new StringBuilder("{\n"); for (Iterator iter = deviceInfos.iterator(); iter.hasNext();) { Object item = iter.next(); deviceInfoStr.append("\t\t\t" + item + ":" + prop.get(item) + ", \n"); } deviceInfoStr.append("}"); return deviceInfoStr.toString(); }
是否有SD卡
1 2 3 4
public static boolean haveSDCard() { return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); }
回到Home,后台运行
1 2 3 4 5 6 7
public staticvoidgoHome(Context context) { Intent mHomeIntent = new Intent(Intent.ACTION_MAIN); mHomeIntent.addCategory(Intent.CATEGORY_HOME); mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); context.startActivity(mHomeIntent); }
获取状态栏高度
注意,要在onWindowFocusChanged中调用,在onCreate中获取高度为0
1 2 3 4 5 6
@TargetApi(Build.VERSION_CODES.CUPCAKE) public static int getStatusBarHeight(Activity activity) { Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); return frame.top; }
获取状态栏高度+标题栏(ActionBar)高度
注意,如果没有ActionBar,那么获取的高度将和上面的是一样的,只有状态栏的高度
1 2 3 4
public static int getTopBarHeight(Activity activity) { return activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT) .getTop(); }
public classConstants{ /** * Unknown network class */ public static final int NETWORK_CLASS_UNKNOWN = 0; /** * wifi net work */ public static final int NETWORK_WIFI = 1; /** * "2G" networks */ public static final int NETWORK_CLASS_2_G = 2; /** * "3G" networks */ public static final int NETWORK_CLASS_3_G = 3; /** * "4G" networks */ public static final int NETWORK_CLASS_4_G = 4; } public static int getNetWorkClass(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); switch (telephonyManager.getNetworkType()) { case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: return Constants.NETWORK_CLASS_2_G; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: return Constants.NETWORK_CLASS_3_G; case TelephonyManager.NETWORK_TYPE_LTE: return Constants.NETWORK_CLASS_4_G; default: return Constants.NETWORK_CLASS_UNKNOWN; } }
判断当前手机的网络类型(WIFI还是2,3,4G)
需要用到上面的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public static int getNetWorkStatus(Context context) { int netWorkType = Constants.NETWORK_CLASS_UNKNOWN; ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { int type = networkInfo.getType(); if (type == ConnectivityManager.TYPE_WIFI) { netWorkType = Constants.NETWORK_WIFI; } elseif (type == ConnectivityManager.TYPE_MOBILE) { netWorkType = getNetWorkClass(context); } } return netWorkType; }
px-dp转换
1 2 3 4 5 6 7 8 9
public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
px-sp转换
1 2 3 4 5 6 7 8 9
public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); }
手机号码正则
1
public static final String REG_PHONE_CHINA = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
邮箱正则
1
public static final String REG_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";