Android: show/hide status bar/power bar

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

I am trying to create a button where I can hide or show the status bar on my tablet.

I've put in the onCreate

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

and in the buttons show:

WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setAttributes(attrs);

hide:

WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setAttributes(attrs);

Any hints/tipps?

//edit

I've looked at this hint here: http://android.serverbox.ch/?p=306 and changed my code like this:

private void hideStatusBar() throws IOException, InterruptedException {     Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","service call activity 79 s16 com.android.systemui"});     proc.waitFor(); }  private void showStatusBar() throws IOException, InterruptedException {     Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"});     proc.waitFor(); }

So if I click on my buttons an the methods are called I can see that something is happening because the app is waiting some seconds. I also looked into LockCat and see that something is happening.

show: http://pastebin.com/CidTRSTi hide: http://pastebin.com/iPS6Kgbp

回答1:

Do you have the fullscreen theme set in the manifest?

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

I don't think you'll be able to go fullscreen without this.

I would use the following to add and remove the fullscreen flag:

// Hide status bar getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); // Show status bar getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);


回答2:

For Some People, Showing status bar by clearing FLAG_FULLSCREEN may not work,

Here is the solution that worked for me, (Documentation) (Flag Reference)

Hide Status Bar

// Hide Status Bar if (Build.VERSION.SDK_INT < 16) {             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                     WindowManager.LayoutParams.FLAG_FULLSCREEN); } else {    View decorView = getWindow().getDecorView();   // Hide Status Bar.    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;    decorView.setSystemUiVisibility(uiOptions); }

Show Status Bar

   if (Build.VERSION.SDK_INT < 16) {               getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);     }     else {        View decorView = getWindow().getDecorView();       // Show Status Bar.        int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;        decorView.setSystemUiVisibility(uiOptions);     }


回答3:

One of the features introduced in KitKat is "Immersive Mode". Immersive mode gives the user the ability to show/hide the status bar and navigation bar with a swipe. To try, click the "Toggle immersive mode" button, then try swiping the bar in and out!

Example:

public void toggleHideyBar() {          int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility();         int newUiOptions = uiOptions;         boolean isImmersiveModeEnabled =                 ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);         if (isImmersiveModeEnabled) {             Log.i(TAG, "Turning immersive mode mode off. ");         } else {             Log.i(TAG, "Turning immersive mode mode on.");         }          // Navigation bar hiding:  Backwards compatible to ICS.         if (Build.VERSION.SDK_INT >= 14) {             newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;         }          // Status bar hiding: Backwards compatible to Jellybean         if (Build.VERSION.SDK_INT >= 16) {             newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;         }          if (Build.VERSION.SDK_INT >= 18) {             newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;         }          getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);         //END_INCLUDE (set_ui_flags)     }


回答4:

Reference - https://developer.android.com/training/system-ui/immersive.html

// This snippet shows the system bars. It does this by removing all the flags // except for the ones that make the content appear under the system bars. private void showSystemUI() {     mDecorView.setSystemUiVisibility(             View.SYSTEM_UI_FLAG_LAYOUT_STABLE             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION             | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!