一个用于去除状态栏和虚拟导航栏的BaseActivity

BaseActivkty

public class BaseActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * change style,hide status bar& navigation bar
         */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(
                new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)) == 0) {
                            onWindowFocusChanged(true);
                        }
                    }
                });
        setContentView(R.layout.activity_main);

        int SDK_VER = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
        if (SDK_VER == 15)
            getWindow().getDecorView().setSystemUiVisibility(4);
        else if (SDK_VER == 16 || SDK_VER == 17)
            getWindow().getDecorView().setSystemUiVisibility(8);
    }

    /**
     * change style,hide status bar& navigation bar
     */

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (!hasFocus) {
            // mBackDown = mHomeDown = false;
        } else if (hasFocus) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

//    /**
//     * 拦截了返回按键逻辑
//     */
//    @Override
//    public boolean dispatchKeyEvent(KeyEvent event) {
//        if (event.getAction() == KeyEvent.ACTION_DOWN) {
//            switch (event.getKeyCode()) {
//            case KeyEvent.KEYCODE_BACK:
//                return true;
//            case KeyEvent.KEYCODE_HOME:
//                return false;
//            }
//        } else if (event.getAction() == KeyEvent.ACTION_UP) {
//            switch (event.getKeyCode()) {
//            case KeyEvent.KEYCODE_BACK:
//                if (!event.isCanceled()) {
//                }
//                return true;
//            case KeyEvent.KEYCODE_HOME:
//                if (!event.isCanceled()) {
//                }
//                return true;
//            }
//        }
//        return super.dispatchKeyEvent(event);
//    }
    
    /**
     * 点击返回按钮时调用的方法
     * @param v
     */
    public void exitOnClick(View v) {
        onBackPressed();
    }
    
}

 

posted @ 2017-08-18 15:57  BlogCommunicator  阅读(305)  评论(0编辑  收藏  举报