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(); } }