android 隐藏Settings菜单的几种方法

本文修改基于mtk6.0系统

隐藏一个一级菜单

  • 源码位置 packages/apps/Settings/src/com/android/settings/SettingsActivity.java;
 private int[] SETTINGS_FOR_RESTRICTED = {
             R.id.wireless_section,
             R.id.wifi_settings,
             R.id.bluetooth_settings,
             R.id.location_settings,
             R.id.security_settings,    
             ...              	
 };

例如隐藏位置项
①修改updateTilesList()方法,添加:

private void updateTilesList(List<DashboardCategory> target) {
    ....
 // Ids are integers, so downcasting is ok
    int id = (int) category.id;
    int n = category.getTilesCount() - 1;
    while (n >= 0) {
       ....
       } else if (id == R.id.location_settings) {
         removeTile=true;
       }
       ....
       if (removeTile && n < category.getTilesCount()) {
           category.removeTile(n);  //移除
       }
       n--;
    }

当removeTile = true时,这个tile所在组会将此tile移除。
②直接注释packages/apps/Settings/res/xml/dashboard_categories.xml中关于位置项

+  <!-- Location -->
   <dashboard-tile
         android:id="@+id/location_settings"
         android:title="@string/location_settings_title"
         android:fragment="com.android.settings.location.LocationSettings"    	 
         android:icon="@drawable/ic_settings_location"
    />
+  -->

mainifest中关于LocationSettings的也要注释掉。

隐藏一个二级菜单

①可直接找到所在xml文件,直接注释

  <PreferenceCategory android:key="device_admin_category"
             android:title="@string/device_admin_title"
             android:persistent="false">

         <Preference android:key="manage_device_admin"
                 android:title="@string/manage_device_admin"
                 android:summary="@string/manage_device_admin_summary"
                 android:persistent="false"
                 android:fragment="com.android.settings.DeviceAdminSettings"/>

+  <!--       <SwitchPreference android:key="toggle_install_applications_validation"
                 android:title="@string/install_validation"
                 android:summaryOn="@string/install_unknown_applications_validation"
                 android:summaryOff="@string/install_unknown_applications_validation"
+                 android:persistent="false" />       -->   

         <SwitchPreference android:key="toggle_install_applications"
                 android:title="@string/install_applications"
                 android:summaryOff="@string/install_unknown_applications"
                 android:summaryOn="@string/install_unknown_applications"
                 android:persistent="false" />

     </PreferenceCategory>

②在代码中移除
deviceAdminCategory.removePreference(mToggleAppInstallationInvalidation);
deviceAdminCategory就是目标所在的PreferenceCategory 。

settings设置中直接移除整个组

如:无线和网络,设备,个人,系统。移除设备组为例

  • 源码位置 packages/apps/Settings/src/com/android/settings/SettingsActivity.java;
 public static void loadCategoriesFromResource(int resid, List<DashboardCategory> target,
             Context context) {
      XmlResourceParser parser = null;
      try {
           parser = context.getResources().getXml(resid);
           AttributeSet attrs = Xml.asAttributeSet(parser);

      int type;
      while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
                     && type != XmlPullParser.START_TAG) {
                 // Parse next until start tag is found
      }
          ...   // dashboard-categories.xml解析 
-
-                    target.add(category);
+                    if (category.key.equals("com.android.settings.category.device")) {
+                        
+                    }else {
+                        target.add(category);
+                    }
                 } else {
                     XmlUtils.skipCurrentTag(parser);
                 }    

你想隐藏哪个组,就不把它加入到List数组中。

  • 四个组所对应的key值在,donottranslate.xml中定义
   * <li>com.android.settings.category.wireless</li>
   * <li>com.android.settings.category.device</li>
   * <li>com.android.settings.category.personal</li>
   * <li>com.android.settings.category.system</li>

测试有效。
settings添加菜单项

你可能感兴趣的:(系统修改)