彷佛Android4.x.x好像要手動受權,其餘的Android系統就不太清楚了 //實現讓狀態欄恢復 public static void showBar() { try { String command; command = "LD_LIBRARY_PATH=endorb:/systemb am startservice -n com.android.systemui/.SystemUIService"; 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 e) { e.printStackTrace(); } } }
//隱藏狀態欄實現全屏 public static void closeBar() { try { String command; command = "LD_LIBRARY_PATH=endorb:/systemb 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) { ex.printStackTrace(); } }
上面的在android4.4上面只須要點擊獲取權限便可實現要求,而在android5.1上面就不行。下面提供一種比上面更好的方法:android
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) }