似乎Android4.x.x好像要手动授权,其他的Android系统就不太清楚了 //实现让状态栏恢复 public static void showBar() { try { String command; command = "LD_LIBRARY_PATH=endorb:/systemb am startservice -n com.android.systemui/.SystemUIService"; ArrayListenvlist = new ArrayList (); Map 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 e) { e.printStackTrace(); } }}
//隐藏状态栏实现全屏public static void closeBar() { try { String command; command = "LD_LIBRARY_PATH=endorb:/systemb service call activity 42 s16 com.android.systemui"; ArrayListenvlist = new ArrayList (); Map 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) { ex.printStackTrace(); }}
上面的在android4.4上面只需要点击获取权限即可实现要求,而在android5.1上面就不行。下面提供一种比上面更好的方法:
public void toggleHideyBar() { // BEGIN_INCLUDE (get_current_ui_flags) // The UI options currently enabled are represented by a bitfield. // getSystemUiVisibility() gives us that bitfield. int uiOptions = getWindow().getDecorView().getSystemUiVisibility(); int newUiOptions = uiOptions; // END_INCLUDE (get_current_ui_flags) // BEGIN_INCLUDE (toggle_ui_flags) boolean isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions); if (isImmersiveModeEnabled) { Log.i("123", "Turning immersive mode mode off. "); } else { Log.i("123", "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; } // Immersive mode: Backward compatible to KitKat. // Note that this flag doesn't do anything by itself, it only augments the behavior // of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample // all three flags are being toggled together. // Note that there are two immersive mode UI flags, one of which is referred to as "sticky". // Sticky immersive mode differs in that it makes the navigation and status bars // semi-transparent, and the UI flag does not get cleared when the user interacts with // the screen. if (Build.VERSION.SDK_INT >= 18) { newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } //getWindow().getDecorView().setSystemUiVisibility(newUiOptions);//上边状态栏和底部状态栏滑动都可以调出状态栏 getWindow().getDecorView().setSystemUiVisibility(4108);//这里的4108可防止从底部滑动调出底部导航栏 //END_INCLUDE (set_ui_flags) }