1.首先在Setting里增加菜单,作为隐藏导航栏的开关
定义字符串 packages/apps/Settings/res/values/strings.xml
<string name="ctrl_navigationbar">Hide NavigationBar</string>
添加菜单项 packages/apps/Settings/res/xml/display_settings.xml
<SwitchPreference android:key="ctrl_navigationbar" android:title="@string/ctrl_navigationbar"/>
增加对应的控制器列表 packages/apps/Settings/src/com/android/settings/DisplaySettings.java
controllers.add(new NavigationBarPreferenceController(context));
增加控制器
packages/apps/Settings/src/com/android/settings/display/NavigationBarPreferenceController.java
package com.android.settings.display; import android.content.Context; import android.provider.Settings; import android.support.v14.preference.SwitchPreference; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceScreen; import com.android.settings.core.PreferenceControllerMixin; import com.android.settingslib.core.AbstractPreferenceController; import android.content.Intent; import android.util.Log; import android.os.SystemProperties; public class NavigationBarPreferenceController extends AbstractPreferenceController implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener { private static final String TAG = "NavigationBarCtrl"; private static final boolean DEBUG = true; private static final String KEY_NAVIGATION_BAR = "ctrl_navigationbar"; public static final String ACTION_HIDE_NAVIGATION_BAR = "com.systemui.navigationbar.hide"; public static final String ACTION_SHOW_NAVIGATION_BAR = "com.systemui.navigationbar.show"; public NavigationBarPreferenceController(Context context) { super(context); } @Override public String getPreferenceKey() { return KEY_NAVIGATION_BAR; } @Override public boolean isAvailable() { return true; } @Override public void displayPreference(PreferenceScreen screen) { if (!isAvailable()) { setVisible(screen, KEY_NAVIGATION_BAR, false /* visible */); return; } final SwitchPreference mNavigationBarPreference = (SwitchPreference)screen.findPreference(KEY_NAVIGATION_BAR); if (mNavigationBarPreference != null) { String value = SystemProperties.get("persist.sys.navigationbar.enable", "false"); mNavigationBarPreference.setChecked(value.equals("true")); mNavigationBarPreference.setOnPreferenceChangeListener(this); } } @Override public void updateState(Preference preference) { String value = SystemProperties.get("persist.sys.navigationbar.enable", "false"); ((SwitchPreference) preference).setChecked(value.equals("true")); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { boolean value = (Boolean) newValue; if (DEBUG) { Log.d(TAG, "key value " + value); } Intent intent = new Intent(); if (value) { intent.setAction(ACTION_SHOW_NAVIGATION_BAR); } else { intent.setAction(ACTION_HIDE_NAVIGATION_BAR); } mContext.sendBroadcast(intent); return true; } }
2.修改SystemUi
frameworks/base/packages/SystemUI/AndroidManifest.xml
<!-- For NavigationBar show or not --> <protected-broadcast android:name="com.systemui.navigationbar.show" /> <protected-broadcast android:name="com.systemui.navigationbar.hide" />
定义广播和对应的系统属性
private static final String ACTION_HIDE_NAVIGATION_BAR = "com.systemui.navigationbar.hide"; private static final String ACTION_SHOW_NAVIGATION_BAR = "com.systemui.navigationbar.show"; private static final String SYS_PROPERTY_NAVIGATION_BAR = "persist.sys.navigationbar.enable";
在start方法里,判断属性值,是否加载状态栏
if (SystemProperties.getBoolean(SYS_PROPERTY_NAVIGATION_BAR, false)) { createNavigationBar(); }
在makeStatusBarView方法里 判断是否加载导航栏
if (SystemProperties.getBoolean(SYS_PROPERTY_NAVIGATION_BAR, false)) { createNavigationBar(); }
广播接收器处理对应逻辑
else if (ACTION_HIDE_NAVIGATION_BAR.equals(action)) { mWindowManager.removeViewImmediate(mNavigationBarView); mNavigationBarView = null; mStatusBarWindow.setVisibility(View.GONE); SystemProperties.set(SYS_PROPERTY_NAVIGATION_BAR, "false"); } else if (ACTION_SHOW_NAVIGATION_BAR.equals(action)) { if(mNavigationBarView == null) createNavigationBar(); else mNavigationBarView.setVisibility(View.VISIBLE); mStatusBarWindow.setVisibility(View.VISIBLE); SystemProperties.set(SYS_PROPERTY_NAVIGATION_BAR, "true"); }