取得安卓手机界面的尺寸,标题栏高度,状态栏高度,虚拟按键存在与否,和虚拟按键高度,取得手机操作系统版本
取得安卓手机界面的尺寸,标题栏高度,状态栏高度,虚拟按键存在与否,和虚拟按键高度,取得手机操作系统版本
1 /**
2 * 获取标题栏的高度
3 *
4 * @param activity
5 * @return
6 */
7 public int getTitleHeight(Activity activity) {
8 Rect rect = new Rect();
9 Window window = activity.getWindow();
10 window.getDecorView().getWindowVisibleDisplayFrame(rect);
11 int statusBarHeight = rect.top;
12 int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
13 int titleBarHeight = contentViewTop - statusBarHeight;
14
15 return titleBarHeight;
16 }
1 /**
2 *
3 * 获取状态栏高度
4 *
5 * @param activity
6 * @return
7 */
8 public int getStateHeight(Activity activity) {
9 Rect rect = new Rect();
10 activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
11 return rect.top;
12 }
1 /**
2 * 获取屏幕宽高
3 *
4 * @param activity
5 * @return int[0] 宽,int[1]高
6 */
7 public int[] getScreenWidthAndSizeInPx(Activity activity) {
8 DisplayMetrics displayMetrics = new DisplayMetrics();
9 activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
10 int[] size = new int[2];
11 size[0] = displayMetrics.widthPixels;
12 size[1] = displayMetrics.heightPixels;
13 return size;
14 }
1 /**
2 * 模拟home键
3 *
4 * @param context
5 */
6 public void goToDestop(Context context) {
7 Intent intent = new Intent(Intent.ACTION_MAIN);
8 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
9 intent.addCategory(Intent.CATEGORY_HOME);
10 context.startActivity(intent);
11 }
1 Android在代码中查看系统版本
2 switch (Build.VERSION.SDK_INT) {
3 case Build.VERSION_CODES.BASE:
4 // API Level 1 Android 1.0
5 Log.i(TAG, "API Level 1 Android 1.0");
6 break;
7 case Build.VERSION_CODES.BASE_1_1:
8 // API Level 2 Android 1.1
9 Log.i(TAG, "API Level 2 Android 1.1");
10 break;
11 case Build.VERSION_CODES.CUPCAKE:
12 // API Level 3 Android 1.5
13 Log.i(TAG, "API Level 3 Android 1.5");
14 break;
15 case Build.VERSION_CODES.DONUT:
16 // API Level 4 Android 1.6
17 Log.i(TAG, "API Level 4 Android 1.6");
18 break;
19 case Build.VERSION_CODES.ECLAIR:
20 // API Level 5 Android 2.0
21 Log.i(TAG, "API Level 5 Android 2.0");
22 break;
23 case Build.VERSION_CODES.ECLAIR_0_1:
24 // API Level 6 Android 2.0.1
25 Log.i(TAG, "API Level 6 Android 2.0.1");
26 break;
27 case Build.VERSION_CODES.ECLAIR_MR1:
28 // API Level 7 Android 2.1.x
29 Log.i(TAG, "API Level 7 Android 2.1.x");
30 break;
31 case Build.VERSION_CODES.FROYO:
32 // API Level 8 Android 2.2.x
33 Log.i(TAG, "API Level 8 Android 2.2.x");
34 break;
35 case Build.VERSION_CODES.GINGERBREAD:
36 // API Level 9 Android 2.3/2.3.1/2.3.2
37 Log.i(TAG, "API Level 9 Android 2.3/2.3.1/2.3.2");
38 break;
39 case Build.VERSION_CODES.GINGERBREAD_MR1:
40 // API Level 10 Android 2.3.4
41 Log.i(TAG, "API Level 10 Android 2.3.4");
42 break;
43 case Build.VERSION_CODES.HONEYCOMB:
44 // API Level 11 Android 3.0.x
45 Log.i(TAG, "API Level 11 Android 3.0.x");
46 break;
47 case Build.VERSION_CODES.HONEYCOMB_MR1:
48 // API Level 12 Android 3.1.x
49 Log.i(TAG, "API Level 12 Android 3.1.x");
50 break;
51 case Build.VERSION_CODES.HONEYCOMB_MR2:
52 // API Level 13 Android 3.2.x
53 Log.i(TAG, "API Level 13 Android 3.2.x");
54 break;
55 case Build.VERSION_CODES.ICE_CREAM_SANDWICH:
56 // API Level 14 Android 4.0/4.0.1/4.0.2
57 Log.i(TAG, "API Level 14 Android 4.0/4.0.1/4.0.2");
58 break;
59 case Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1:
60 // API Level 15 Android 4.0.3/4.0.4
61 Log.i(TAG, "API Level 15 Android 4.0.3/4.0.4");
62 break;
63 case Build.VERSION_CODES.JELLY_BEAN:
64 // API Level 16 Android 4.1/4.1.1
65 Log.i(TAG, "API Level 16 Android 4.1/4.1.1");
66 break;
67 case Build.VERSION_CODES.JELLY_BEAN_MR1:
68 // API Level 17 Android 4.2/4.2.2
69 Log.i(TAG, "API Level 17 Android 4.2/4.2.2");
70 break;
71 case Build.VERSION_CODES.JELLY_BEAN_MR2:
72 // API Level 18 Android 4.3
73 Log.i(TAG, "API Level 18 Android 4.3");
74 break;
75 case Build.VERSION_CODES.KITKAT:
76 // API Level 19 Android 4.4
77 Log.i(TAG, "API Level 19 Android 4.4");
78 break;
79 default:
80 break;
81 }