import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.os.Handler; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; /** * @author Dawid Drozd ** Please check before adding this fragment simply call * isFragmentAvailable. *
* You must manually call onKeyDown and onWindowFocusChanged. */ public abstract class FullScreenActivity extends Activity implements Runnable { //全屏使用的flag private static final int UI_FLAGS = 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; GestureDetector mGesture = null; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGesture = new GestureDetector(this, new GestureListener()); final View decorView = getWindow().getDecorView(); decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { // System.out.println("=====onSystemUiVisibilityChange======="); if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { setImmersiveMode(); // System.out.println("system bars are visible"); } } }); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { mGesture.onTouchEvent(ev); return super.dispatchTouchEvent(ev); } @Override protected void onResume() { super.onResume(); setImmersiveMode(); } @Override public void onWindowFocusChanged(boolean hasFocus) { // System.out.println("onWindowFocusChanged"); setImmersiveMode(); _handler.removeCallbacks(this); _handler.postDelayed(this, 20); } @Override public void onStop() { _handler.removeCallbacks(this); super.onStop(); } @Override public void run() { setImmersiveMode(); } @SuppressLint("NewApi") public void setImmersiveMode() { setImmersiveMode(this); } @SuppressLint("NewApi") public void setImmersiveMode(Activity activity) { // System.out.println("===============setImmersiveMode==============="); activity.getWindow().getDecorView().setSystemUiVisibility(UI_FLAGS); } private Handler _handler = new Handler() ; private void addView() { // System.out.println("============addView ============"); AlertDialog builder = new AlertDialog.Builder(this).create(); builder.setTitle("测试"); builder.show(); builder.dismiss(); } class GestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //判断导航栏是否弹出,这是在横屏情况下 System.out.println("========onFling========"); if (e1.getY() > e2.getY() && e1.getY() > 1480) {//从下往上滑 addView(); } else if (e1.getY() < e2.getY() && e1.getY() < 70) { //从上往下滑 addView(); } return super.onFling(e1, e2, velocityX, velocityY); } } @Override protected void onDestroy() { super.onDestroy(); } }