先写在前面,这说的Settings加载选项是指Settings这个应用显示在主界面的选项,这个修改需要对系统源码进行修改。
Android 7.0 Settings顶部多了一个建议选项,多了个侧边栏,操作更加便捷了。
之前做Android 6.0开发的,都会了解到6.0的Settings加载选项是通过加载dashboard_categories.xml,获取需要显示的选项,并且在SettingsActivity中也进行判断是否要显示,所以在6.0上添加一个选项是比较简单的,直接在dashboard_categories.xml添加icon、title、summary,也可以添加目标fragment和Intent,这样就可以顺利跳转到对应的界面了。但是在7.0上,google对Settings进行了重构。
7.0的Settings的选项不再从dashboard_categories.xml中加载选项列表,而是通过在Androidmanifest.xml中,配置intent-filter的Action,在通过PackageManager进行指定的Action进行搜索,那么就可以获取到需要显示的选项列表了,并且也需要在代码中进行判断,判断哪些功能需要显示与否。
TileUtils.java中通过几个Action进行获取系统中对应的activity,如Settings中的几个
private static final String SETTINGS_ACTION =
"com.android.settings.action.SETTINGS";
private static final String OPERATOR_SETTINGS =
"com.android.settings.OPERATOR_APPLICATION_SETTING";
private static final String OPERATOR_DEFAULT_CATEGORY =
"com.android.settings.category.wireless";
private static final String MANUFACTURER_SETTINGS =
"com.android.settings.MANUFACTURER_APPLICATION_SETTING";
private static final String MANUFACTURER_DEFAULT_CATEGORY =
"com.android.settings.category.device";
通过PackageManager进行搜索,获取到这一系列的activity信息,
PackageManager pm = context.getPackageManager();
List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
PackageManager.GET_META_DATA, user.getIdentifier());
并且在AndroidManifest.xml通过meta-data配置了icon、title、summary,那这就有Settings中显示的图标、标题和说明。还有在Settings中显示的分类、目标Fragment。
Settings 的AndroidManifest.xml 下面那代码是Settings->about phone的Activity配置。
<activity android:name="Settings$DeviceInfoSettingsActivity"
android:theme="@style/Theme.SubSettingsDialogWhenLarge"
android:label="@string/device_info_settings"
android:icon="@drawable/ic_settings_about"
android:taskAffinity="com.android.settings"
android:parentActivityName="Settings">
<intent-filter android:priority="1">
<action android:name="android.settings.DEVICE_INFO_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE_LAUNCH" />
intent-filter>
<intent-filter android:priority="-1">
<action android:name="com.android.settings.action.SETTINGS" />
intent-filter>
<meta-data android:name="com.android.settings.category"
android:value="com.android.settings.category.system" />
<meta-data android:name="com.android.settings.title"
android:resource="@string/about_settings" />
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.DeviceInfoSettings" />
<meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
android:value="true" />
activity>
在SettingsActivity.java的ENTRY_FRAGMENTS中,根据fragment className进行fragment是否有效的判断。
private static final String[] ENTRY_FRAGMENTS = {
...
DeviceInfoSettings.class.getName(),
...
}
Settings显示各选项的思路比较简单,所以直接在Settings里面添加功能选项还是比较简单的。
如果需要Settings中添加打包好的apk,需要三个步骤。
private static final String EXTRA_SETTINGS_ACTION =
"com.android.settings.action.EXTRA_SETTINGS";
/**
* 在settings中显示在哪个分类中
* The key used to get the category from metadata of activities of action
* {@link #EXTRA_SETTINGS_ACTION}
* The value must be one of:
* com.android.settings.category.wireless
* com.android.settings.category.device
* com.android.settings.category.personal
* com.android.settings.category.system
*/
private static final String EXTRA_CATEGORY_KEY = "com.android.settings.category";
/**
* 在settings中显示的图标
* Name of the meta-data item that should be set in the AndroidManifest.xml
* to specify the icon that should be displayed for the preference.
*/
public static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon";
/**
* 在settings中显示的标题
* Name of the meta-data item that should be set in the AndroidManifest.xml
* to specify the title that should be displayed for the preference.
*/
public static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title";
/**
* 在settings中显示的描述语句
* Name of the meta-data item that should be set in the AndroidManifest.xml
* to specify the summary text that should be displayed for the preference.
*/
public static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary";
/// Extra package white list for add item to Settings @{
private static final String[] EXTRA_PACKAGE_WHITE_LIST = {};
/// @}
最后就是全编,刷机验证了。
相比android6.0,在7.0上添加功能选项显得更简单了,只需要写好了功能,在Settings的AndroidManifest.xml中添加必要的参数,或者是在独立的apk中AndroidManifest.xml中配置必要的参数,再在TileUtils.java中添加包名就好了。需要修改的地方更少了,并且Google将很方方法放到了com.android.settinglibs里,精简了一部分代码。