新需求:删除Android 原生自带的虚拟导航按键(暂定)
如想直接删除不显示Navigationbar,可以用如下两个方法
1)在资源xml文件中把导航按键的高度和宽度设置为0
frameworks/base/core/res/res/values/dimens.xml
0dp
0dp
在配置文件中设置config_showNavigationBar=false
frameworks/base/core/res/res/values/config.xml设置为false
SystemProperties.get("qemu.hw.mainkeys");1为关,0为开
源码判断方法
mHasNavigationBar = res.getBoolean(com.android.internal.R.bool.config_showNavigationBar);
// Allow a system property to override this. Used by the emulator.
// See also hasNavigationBar().
String navBarOverride = SystemProperties.get("qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
mHasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
mHasNavigationBar = true;
}
码农最怕有些功能删除又要添加,为了方便起见除了在overlay配置外,可以选择发广播或者数据库方式来动态控制显示或者隐藏Navigationbar。我这边选择了在开发者选项中增加了动态显示隐藏开关。
我在开发者选项中增加了显示隐藏开关如图所示
1)先在数据库中增加自己设定的一个字段来显示是否打开关闭虚拟按键
frameworks/base/packages/SettingsProvider/res/values/defaluts.xml
false
Java文件中
frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java注意在settings表中增加
//<17-7-15> modify start
******
loadSetting(stmt, "switch_navigation_bar",R.string.switch_navigation_bar);
******
再onCreate中初始化按钮,获取数据库的值(自定义kay),来显示当前状态是否是打开或关闭状态
//<17-8-2> modify start
mNavigationbarView=(CheckBoxPreference) findPreference("switch_navigationbarview");
if(null != mNavigationbarView){
String isShownav = Settings.System.getString(getContentResolver(), "switch_navigation_bar");
mNavigationbarView.setChecked("true".equals(isShownav)?true:false);
mNavigationbarView.setOnPreferenceChangeListener(this);
Log.i("mengshirui", "AccessibilitySettings.initializeAllPreferences: setChecked"+isShownav);
}
//<17-8-2> modify end
//<17-8-2> modify start
}else if(preference == mNavigationbarView){
boolean auto = (Boolean)newValue;
Settings.System.putString(getContentResolver(),"switch_navigation_bar",String.valueOf(auto));
return true;
//<17-8-2> modify end
4)在PhoneStatusBar.java中来实现动态添加或者隐藏虚拟按键,可以用接受广播的方式或者监听数据库的变化。
在makeStatusBarView() {
SettingsValueChangeContentObserver mNavContentObserver = new SettingsValueChangeContentObserver();
mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor("switch_navigation_bar"),true,mNavContentObserver);
}
/**
* <17-7-15> modify start
*/
class SettingsValueChangeContentObserver extends ContentObserver{
public SettingsValueChangeContentObserver() {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
String isShownav = Settings.System.getString(mContext.getContentResolver(), "switch_navigation_bar");
if ("true".equals(isShownav)) {
if (mNavigationBarView == null) {
addNarBar();
}
if (mNavigationBarView != null) {
checkBarMode(MODE_TRANSLUCENT, mNavigationBarWindowState, mNavigationBarView.getBarTransitions());
}
} else {
mWindowManager.removeView(mNavigationBarView);
mNavigationBarView = null;
}
}
}
private void addNarBar() {
if (mNavigationBarView == null) {
mNavigationBarView = (NavigationBarView) View.inflate(mContext, R.layout.navigation_bar, null);
mNavigationBarView.setDisabledFlags(mDisabled);
mNavigationBarView.setBar(this);
prepareNavigationBarView();
}
mWindowManager.addView(mNavigationBarView, getNavigationBarLayoutParams());
}
//<17-7-16> modify end