项目:K560_MTK6761_Q_Go
settings模块还是要从AndroidManifest.xml出发,MTK的项目一般会用自己的MTKSettings
可以看到,Android Q项目的settings模块和之前的差别还是很明显的。
Android Q:
<!-- Alias for launcher activity only, as this belongs to each profile. -->
<activity-alias android:name="Settings"
android:label="@string/settings_label_launcher"
android:launchMode="singleTask"
android:targetActivity=".homepage.SettingsHomepageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
</activity-alias>
Android O:
<!-- Alias for launcher activity only, as this belongs to each profile. -->
<activity-alias android:name="Settings"
android:taskAffinity="com.android.settings" //和singleTask启动模式有关
android:label="@string/settings_label_launcher"
android:launchMode="singleTask"
android:targetActivity="Settings">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
</activity-alias>
可以看到activity-alias只是一个别名,常用的方式是为应用增加一个桌面快捷图标。它实际指向的应用是targetActivity,Android Q项目指向的是SettingsHomepageActivity,而Android O项目指向的是Settings
SettingsHomepageActivity里面加载了layout,R.layout.settings_homepage_container,这个布局里面主要就包括设置模块上面的搜索栏,和中间的主菜单列表的容器等。之后会调用showFragment(new TopLevelSettings(), R.id.main_content)这个方法,来显示中间的主菜单列表。通过TopLevelSettings这个类。
TopLevelSettings中的getPreferenceScreenResId()方法加载xml资源文件,加载的是top_level_settings。
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:key="top_level_settings">
<Preference
android:key="top_level_wikofeature"
android:title="@string/wios_feature_settings"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_home_wikofeatures"
android:order="-130"
android:fragment="com.android.settings.wiosfeature.WiosFeatureDashboardFragment"
settings:controller="com.android.settings.wiosfeature.TopLevelWiosFeaturePreferenceController"/>
<Preference
android:key="top_level_network"
android:title="@string/network_dashboard_title"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_network"
android:order="-120"
android:fragment="com.android.settings.network.NetworkDashboardFragment"
settings:controller="com.android.settings.network.TopLevelNetworkEntryPreferenceController"/>
<Preference
android:key="top_level_connected_devices"
android:title="@string/connected_devices_dashboard_title"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_connected_device"
android:order="-110"
android:fragment="com.android.settings.connecteddevice.ConnectedDeviceDashboardFragment"
settings:controller="com.android.settings.connecteddevice.TopLevelConnectedDevicesPreferenceController"/>
<Preference
android:key="top_level_apps_and_notifs"
android:title="@string/app_and_notification_dashboard_title"
android:summary="@string/app_and_notification_dashboard_summary"
android:icon="@drawable/ic_homepage_apps"
android:order="-100"
android:fragment="com.android.settings.applications.AppAndNotificationDashboardFragment"/>
<Preference
android:key="top_level_battery"
android:title="@string/power_usage_summary_title"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_battery"
android:fragment="com.android.settings.fuelgauge.PowerUsageSummary"
android:order="-90"
settings:controller="com.android.settings.fuelgauge.TopLevelBatteryPreferenceController"/>
<Preference
android:key="top_level_display"
android:title="@string/display_settings"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_display"
android:order="-80"
android:fragment="com.android.settings.DisplaySettings"
settings:controller="com.android.settings.display.TopLevelDisplayPreferenceController"/>
<Preference
android:key="top_level_sound"
android:title="@string/sound_settings"
android:summary="@string/sound_dashboard_summary"
android:icon="@drawable/ic_homepage_sound"
android:order="-70"
android:fragment="com.android.settings.notification.SoundSettings"/>
<Preference
android:key="top_level_storage"
android:title="@string/storage_settings"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_storage"
android:order="-60"
android:fragment="com.android.settings.deviceinfo.StorageSettings"
settings:controller="com.android.settings.deviceinfo.TopLevelStoragePreferenceController"/>
<Preference
android:key="top_level_privacy"
android:title="@string/privacy_dashboard_title"
android:summary="@string/privacy_dashboard_summary"
android:icon="@drawable/ic_homepage_privacy"
android:order="-55"
android:fragment="com.android.settings.privacy.PrivacyDashboardFragment"/>
<Preference
android:key="top_level_location"
android:title="@string/location_settings_title"
android:summary="@string/location_settings_loading_app_permission_stats"
android:icon="@drawable/ic_homepage_location"
android:order="-50"
android:fragment="com.android.settings.location.LocationSettings"
settings:controller="com.android.settings.location.TopLevelLocationPreferenceController"/>
<Preference
android:key="top_level_security"
android:title="@string/security_settings_title"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_security"
android:order="-40"
android:fragment="com.android.settings.security.SecuritySettings"
settings:controller="com.android.settings.security.TopLevelSecurityEntryPreferenceController"/>
<Preference
android:key="top_level_accounts"
android:title="@string/account_dashboard_title"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_accounts"
android:order="-30"
android:fragment="com.android.settings.accounts.AccountDashboardFragment"
settings:controller="com.android.settings.accounts.TopLevelAccountEntryPreferenceController"/>
<Preference
android:key="top_level_accessibility"
android:title="@string/accessibility_settings"
android:summary="@string/accessibility_settings_summary"
android:icon="@drawable/ic_homepage_accessibility"
android:order="-20"
android:fragment="com.android.settings.accessibility.AccessibilitySettings"
settings:controller="com.android.settings.accessibility.TopLevelAccessibilityPreferenceController"/>
<Preference
android:key="top_level_system"
android:title="@string/header_category_system"
android:summary="@string/system_dashboard_summary"
android:icon="@drawable/ic_homepage_system_dashboard"
android:order="10"
android:fragment="com.android.settings.system.SystemDashboardFragment"
settings:controller="com.android.settings.wiosfeature.TopLevelSystemDashboardPreferenceController"/>
<Preference
android:key="top_level_about_device"
android:title="@string/about_settings"
android:summary="@string/summary_placeholder"
android:icon="@drawable/ic_homepage_about"
android:order="20"
android:fragment="com.android.settings.deviceinfo.aboutphone.MyDeviceInfoFragment"
settings:controller="com.android.settings.deviceinfo.aboutphone.TopLevelAboutDevicePreferenceController"/>
<Preference
android:key="top_level_support"
android:summary="@string/support_summary"
android:title="@string/page_tab_title_support"
android:icon="@drawable/ic_homepage_support"
android:order="100"
settings:controller="com.android.settings.support.SupportPreferenceController"/>
</PreferenceScreen>
从资源文件中可以看到,这个资源文件的结构基本上就是主菜单列表的结构,添加一个一级菜单也可以直接在这个里添加,这和之前的项目差别,像Android O的项目增加一级菜单是直接在AndroidManifest.xml添加就行了,而Android Q的直接在这里面添加好像是不行的(我没有成功)