当前位置: 代码迷 >> 综合 >> [RK3399][Android7.1] 调试笔记 --- 移除/删除Settings的Preference的三种方法小结
  详细解决方案

[RK3399][Android7.1] 调试笔记 --- 移除/删除Settings的Preference的三种方法小结

热度:95   发布时间:2023-10-09 13:16:43.0

Platform: RK3399-MID
OS: Android 7.1.2
Kernel: v4.4.83

需求:

整理一下移除/删除Settings的Preference三种方法;

方法一:对于 PreferenceScreen采用removePreference或者removeAll删除;

[RK3399][Android7.1] 调试笔记 --- 移除/删除Settings的Preference的三种方法小结

[RK3399][Android7.1] 调试笔记 --- 移除/删除Settings的Preference的三种方法小结

上图图1中的“字体大小”以及图2的“系统”称为一级节点,图2的“字体大小”称为二级节点;

先附上xml文件;

packages/apps/Settings/res/xml/display_settings.xml

        <PreferenceScreenandroid:key="font_size"android:title="@string/title_font_size"android:fragment="com.android.settings.accessibility.ToggleFontSizePreferenceFragment"settings:keywords="@string/keywords_display_font_size" /><com.android.settings.display.ScreenZoomPreferenceandroid:key="screen_zoom"android:title="@string/screen_zoom_title"settings:keywords="@string/screen_zoom_keywords" /><DropDownPreferenceandroid:key="auto_rotate"android:summary="%s"android:title="@string/display_auto_rotate_title" />

packages/apps/Settings/res/xml/accessibility_settings.xml

    <PreferenceCategoryandroid:key="system_category"android:title="@string/accessibility_system_title"><PreferenceScreenandroid:fragment="com.android.settings.accessibility.CaptionPropertiesFragment"android:key="captioning_preference_screen"android:title="@string/accessibility_captioning_title" /><PreferenceScreenandroid:fragment="com.android.settings.accessibility.ToggleScreenMagnificationPreferenceFragment"android:key="screen_magnification_preference_screen"android:title="@string/accessibility_screen_magnification_title"/><PreferenceScreenandroid:fragment="com.android.settings.accessibility.ToggleFontSizePreferenceFragment"android:key="font_size_preference_screen"android:title="@string/title_font_size"/><com.android.settings.display.ScreenZoomPreferenceandroid:key="screen_zoom"android:title="@string/screen_zoom_title"/>

PreferenceCategory和PreferenceScreen都属于PreferenceGroup的子类,PreferenceCategory可以用于分类,且不可被点击;

对于PreferenceCategory,有如下两个API用于移除Preference;

public boolean removePreference(Preference preference)  //从group中移除  返回是否移除成功
public void removeAll( ) //移除group中所有的preference

对于PreferenceScreen的一级节点,则可以使用以下API进行移除;

Preference p = findPreference(KEY_LED_SETTINGS);
getPreferenceScreen().removePreference(p);

 也可以采用如下的方式;

removePreference(KEY_LED_SETTINGS);

附上Patch

diff --git a/src/com/android/settings/accessibility/AccessibilitySettings.java b/src/com/android/settings/accessibility/AccessibilitySettings.java
old mode 100644
new mode 100755
index 62671fa..e936dc3
--- a/src/com/android/settings/accessibility/AccessibilitySettings.java
+++ b/src/com/android/settings/accessibility/AccessibilitySettings.java
@@ -214,6 +214,7 @@ public class AccessibilitySettings extends SettingsPreferenceFragment implementssuper.onCreate(icicle);addPreferencesFromResource(R.xml.accessibility_settings);initializeAllPreferences();
+        mSystemsCategory.removePreference(mFontSizePreferenceScreen);mDpm = (DevicePolicyManager) (getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE));}
diff --git a/src/com/android/settings/DisplaySettings.java b/src/com/android/settings/DisplaySettings.java
index 78e0647..dea9465 100755
--- a/src/com/android/settings/DisplaySettings.java
+++ b/src/com/android/settings/DisplaySettings.java
@@ -253,6 +253,7 @@ public class DisplaySettings extends SettingsPreferenceFragment implementsmNightModePreference.setValue(String.valueOf(currentNightMode));mNightModePreference.setOnPreferenceChangeListener(this);}
+        getPreferenceScreen().removePreference(mFontSizePref);}

方法二:对于 WIFI、蓝牙等处于设置第一页的设置项

直接上Patch

diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 152a949..3dacbf6 100755
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -1062,7 +1062,7 @@ public class SettingsActivity extends SettingsDrawerActivityString packageName = getPackageName();setTileEnabled(new ComponentName(packageName, WifiSettingsActivity.class.getName()),
-                pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin, pm);
+                pm.hasSystemFeature(PackageManager.FEATURE_WIFI) && false, isAdmin, pm);setTileEnabled(new ComponentName(packageName,Settings.BluetoothSettingsActivity.class.getName()),

方法三:对于直接在xml文件中绘制的UI界面

直接上Patch

diff --git a/res/xml/accessibility_settings.xml b/res/xml/accessibility_settings.xml
old mode 100644
new mode 100755
index 11cde5e..7f96032
--- a/res/xml/accessibility_settings.xml
+++ b/res/xml/accessibility_settings.xml
@@ -43,9 +43,9 @@android:key="font_size_preference_screen"android:title="@string/title_font_size"/>-        <com.android.settings.display.ScreenZoomPreference
+        <!--<com.android.settings.display.ScreenZoomPreferenceandroid:key="screen_zoom"
-            android:title="@string/screen_zoom_title"/>
+            android:title="@string/screen_zoom_title"/>--><PreferenceScreen

  相关解决方案