BaseActivity.java
5.09 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.sunvote.xpadapp.activity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import com.sunvote.util.LogUtil;
import com.sunvote.util.XPadSystem;
/**
* Created by Elvis on 2017/11/30 11:24
* Email:Eluis@psunsky.com
* 版权所有:长沙中天电子设计开发有限公司
* Description: 人大通用版XPadAppRD重构
*/
public class BaseActivity extends FragmentActivity {
private String TAG = "BaseActivity";
public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000; //需要自己定义标志
private static final int STORAGE_REQUEST_CODE = 102;
private static final int STORAGE_READ_REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);//关键代码
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onCreate(savedInstanceState);
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, STORAGE_REQUEST_CODE);
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, STORAGE_READ_REQUEST_CODE);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
LogUtil.i(TAG,"onKeyDown keycode="+keyCode );
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
LogUtil.i(TAG,"KEYCODE_BACK");
return true;
case KeyEvent.KEYCODE_HOME:
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
//返回true,不响应其他key
int keyCode= event.getKeyCode();
LogUtil.i(TAG,"dispatchKeyEvent keycode="+keyCode );
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
LogUtil.i(TAG,"KEYCODE_BACK");
return true;
case KeyEvent.KEYCODE_HOME:
if(event.isLongPress()){
moveTaskToBack(false);
}
return true;
}
return true;
}
public boolean checkSelfPermission(String permission, int requestCode) {
LogUtil.i(getClass().getSimpleName(), "checkSelfPermission " + permission + " " + requestCode);
if (ContextCompat.checkSelfPermission(this,
permission)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{permission},
requestCode);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case STORAGE_REQUEST_CODE:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
finish();
}
break;
case STORAGE_READ_REQUEST_CODE:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
}
}
public void hideBottomUIMenu() {
// 隐藏虚拟按键,并且全屏
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | 0x00002000 ;
decorView.setSystemUiVisibility(uiOptions);
}
XPadSystem.setNavgationGone(this);
}
public void showBottomUIMenu() {
// 隐藏虚拟按键,并且全屏
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY //| View.SYSTEM_UI_FLAG_FULLSCREEN
& ~0x00002000;
decorView.setSystemUiVisibility(uiOptions);
}
XPadSystem.setNavgationVisible(this);
}
@Override
protected void onResume() {
super.onResume();
hideBottomUIMenu();
}
@Override
protected void onDestroy() {
super.onDestroy();
showBottomUIMenu();
}
}