以下內容來自多個開源專案的整理和自己的專案積累
1.收集裝置資訊,用於資訊統計分析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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 static String collectDeviceInfoStr(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(); } |
2.是否有SD卡
1 2 3 4 |
public static boolean haveSDCard() { return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); } |
3.動態隱藏軟鍵盤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@TargetApi(Build.VERSION_CODES.CUPCAKE) public static void hideSoftInput(Activity activity) { View view = activity.getWindow().peekDecorView(); if (view != null) { InputMethodManager inputmanger = (InputMethodManager) activity .getSystemService(Context.INPUT_METHOD_SERVICE); inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0); } } @TargetApi(Build.VERSION_CODES.CUPCAKE) public static void hideSoftInput(Context context, EditText edit) { edit.clearFocus(); InputMethodManager inputmanger = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); inputmanger.hideSoftInputFromWindow(edit.getWindowToken(), 0); } |
4.動態顯示軟鍵盤
1 2 3 4 5 6 7 8 9 |
@TargetApi(Build.VERSION_CODES.CUPCAKE) public static void showSoftInput(Context context, EditText edit) { edit.setFocusable(true); edit.setFocusableInTouchMode(true); edit.requestFocus(); InputMethodManager inputManager = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(edit, 0); } |
5.動態顯示或者是隱藏軟鍵盤
1 2 3 4 5 6 7 8 9 |
@TargetApi(Build.VERSION_CODES.CUPCAKE) public static void toggleSoftInput(Context context, EditText edit) { edit.setFocusable(true); edit.setFocusableInTouchMode(true); edit.requestFocus(); InputMethodManager inputManager = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } |
6.主動回到Home,後臺執行
1 2 3 4 5 6 7 |
public static void goHome(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); } |
7.獲取狀態列高度
注意,要在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; } |
8.獲取狀態列高度+標題欄(ActionBar)高度
(注意,如果沒有ActionBar,那麼獲取的高度將和上面的是一樣的,只有狀態列的高度)
1 2 3 4 |
public static int getTopBarHeight(Activity activity) { return activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT) .getTop(); } |
9.獲取MCC+MNC程式碼 (SIM卡運營商國家程式碼和運營商網路程式碼)
僅當使用者已在網路註冊時有效, CDMA 可能會無效(中國移動:46000 46002, 中國聯通:46001,中國電信:46003)
1 2 3 4 5 |
public static String getNetworkOperator(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); return telephonyManager.getNetworkOperator(); } |
10.返回行動網路運營商的名字
(例:中國聯通、中國移動、中國電信) 僅當使用者已在網路註冊時有效, CDMA 可能會無效)
1 2 3 4 5 |
public static String getNetworkOperatorName(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); return telephonyManager.getNetworkOperatorName(); } |
11.返回移動終端型別
PHONE_TYPE_NONE :0 手機制式未知
PHONE_TYPE_GSM :1 手機制式為GSM,移動和聯通
PHONE_TYPE_CDMA :2 手機制式為CDMA,電信
PHONE_TYPE_SIP:3
1 2 3 4 5 |
public static int getPhoneType(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); return telephonyManager.getPhoneType(); } |
12.判斷手機連線的網路型別(2G,3G,4G)
聯通的3G為UMTS或HSDPA,移動和聯通的2G為GPRS或EGDE,電信的2G為CDMA,電信的3G為EVDO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
public class Constants { /** * 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; } } |
13.判斷當前手機的網路型別(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; } else if (type == ConnectivityManager.TYPE_MOBILE) { netWorkType = getNetWorkClass(context); } } return netWorkType; } |