Android studio hides the navigation bar to achieve full screen

public class MainActivity extends AppCompatActivity 
  • In the class where you need to hide the navigation bar, replace BaseActivity with AppCompatActivity
public class MainActivity extends BaseActivity
  • Implementation class
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.util.ArrayList;
import java.util.Map;
public class BaseActivity extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        hideNavigationBar();
        closeBar();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
    
    
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus){
    
    
            hideNavigationBar();
        }

    }

    public void hideNavigationBar() {
    
    
        int uiFlags =
                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 // hide nav bar
                | View.SYSTEM_UI_FLAG_FULLSCREEN// hide status bar
                ;
        if( android.os.Build.VERSION.SDK_INT >= 19 ){
    
    
            uiFlags |= 0x00001000;    //SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide navigation bars - compatibility: building API level is lower thatn 19, use magic number directly for higher API target level
        } else {
    
    
            uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }

      getWindow().getDecorView().setSystemUiVisibility(uiFlags);

//        getWindow().getDecorView().setSystemUiVisibility(4108);//这里的4108可防止从底部滑动调出底部导航栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
    
    
            actionBar.hide();
        }
    }
    /**
     * 关闭Android导航栏,实现全屏
     */
    private void closeBar() {
    
    
        try {
    
    
            String command;
            command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";
            ArrayList<String> envlist = new ArrayList<String>();
            Map<String, String> env = System.getenv();
            for (String envName : env.keySet()) {
    
    
                envlist.add(envName + "=" + env.get(envName));
            }
            String[] envp = envlist.toArray(new String[0]);
            Process proc = Runtime.getRuntime().exec(
                    new String[] {
    
     "su", "-c", command }, envp);
            proc.waitFor();
        } catch (Exception ex) {
    
    
        }
    }

}

Guess you like

Origin blog.csdn.net/afufufufu/article/details/130380110