web-dev-qa-db-fra.com

Android: afficher/masquer la barre d'état/barre d'alimentation

J'essaie de créer un bouton où je peux masquer ou afficher la barre d'état sur ma tablette.

J'ai mis dans le onCreate

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

et dans les boutonsshow: 

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

cacher:

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

Des astuces/conseils?

//modifier

J'ai regardé cet indice ici: http://Android.serverbox.ch/?p=306 Et changé mon code comme suit:

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

Donc, si je clique sur mes boutons et que les méthodes s'appellent, je peux voir que quelque chose se passe parce que l'application attend quelques secondes… .. J'ai aussi examiné LockCat et voir que quelque chose se passe.

show: http://Pastebin.com/CidTRSTi masquer: http://Pastebin.com/iPS6Kgbp

30
B770

Avez-vous le thème plein écran défini dans le manifeste?

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

Je ne pense pas que vous pourrez aller en plein écran sans cela.

Je voudrais utiliser ce qui suit pour ajouter et supprimer le drapeau plein écran:

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

Pour certaines personnes, l'affichage de la barre d'état en effaçant FLAG_FULLSCREEN peut ne pas fonctionner, 

Voici la solution qui a fonctionné pour moi, ( Documentation ) ( Référence du drapeau

Masquer la barre d'état

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

Afficher la barre d'état

   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);
    }
32
Pradeep

utilisé pour kolin dans Android pour masquer la barre d'état dans kolin pas besoin d'utiliser un point-virgule (;) à la fin de la ligne

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

dans Android en utilisant Java langage pour la barre d'état masquée

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
1
Prabh deep

avec cette méthode, en utilisant SYSTEM_UI_FLAG_IMMERSIVE_STICKY, le plein écran revient en un clic sans aucune implémentation. Copiez simplement la méthode ci-dessous et appelez-la où vous voulez dans votre activité. Plus de détails ici

private void hideSystemUI() {
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_IMMERSIVE
                    // Set the content to appear under the system bars so that the
                    // content doesn't resize when the system bars hide and show.
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    // Hide the nav bar and status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
}
0
Nicoolasens
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //hide status bar
    requestWindowFeature( Window.FEATURE_NO_TITLE );
    getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN );


    setContentView(R.layout.activity_main);
}
0
Nihal Desai

Référence - 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);
}
0
Varun Bhatia
fun Activity.setStatusBarVisibility(isVisible: Boolean) {
    //see details https://developer.Android.com/training/system-ui/immersive
    if (isVisible) {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    } else {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }
}
0
Yazon2006

utilisé pour kolin dans Android pour masquer la barre d'état dans kolin pas besoin d'utiliser un point-virgule (;) à la fin de la ligne 

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

dans Android en utilisant le langage Java pour la barre d'état cachée

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
0
Prabh deep