前一段时间一直在看Android的Settings源码,现在来总结下。我只总结下基本的流程,至于具体的分析我会给出我看过的比较优秀的博文的链接。
一、Settings源码的路径
Settings路径:packages\apps\Settings
由此路径可以看出Settings也是一个app,只不过是一个系统的app,由于是一个app所以可以单编。
二、Settings的主界面
我们先看看清单文件,来找到settings的启动类。(我看的是Android4.4的Settings源码)
...
<!-- Settings -->
<activity
android:name="Settings"
android:label="@string/settings_label"
android:launchMode="singleTask"
android:taskAffinity="com.android.settings"
android:uuiTheme="@style/SettingsTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.settings.SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
1.com.android.settings.Settings.java是启动类,下面我们去看看启动类:
public class Settings extends PreferenceActivity implements ButtonBarHandler, OnAccountsUpdateListener
实现onBuildHeaders()回调用来指定头文件,即加载界面对应的Fragment
调用loadHeadersFromResource()方法对界面进行加载
@Override
public void onBuildHeaders(List<Header> headers) {
loadHeadersFromResource(R.xml.settings_headers, headers);
updateHeaderList(headers);
mHeaders = headers;
}
2.settings主界面UI布局
Setting是采用PreferenceFragment的形式进行布局的。
它的布局文件是xml文件夹下的:settings_headers.xml。这个布局文件下面全是<Header></Header>
标签。
settings_header.xml对应的代码为:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<!-- WIRELESS and NETWORKS -->
<header android:id="@+id/wireless_section" android:title="@string/header_category_wireless_networks" />
<!-- Wifi -->
<!-- MStar Android Patch Begin -->
<header android:id="@+id/wifi_settings" android:fragment="com.android.settings.wifi.TvWifiSettings" android:title="@string/wifi_settings_title" android:icon="@drawable/ic_settings_wireless" />
<!-- MStar Android Patch End -->
<!-- Ethernet -->
<header android:id="@+id/ethernet_settings" android:fragment="com.android.settings.ethernet.EthernetSettings" android:title="@string/ethernet_status_title" android:icon="@drawable/ic_settings_ethernet" />
<!-- Bluetooth -->
<header android:id="@+id/bluetooth_settings" android:fragment="com.android.settings.bluetooth.TvBluetoothSettings" android:title="@string/bluetooth_settings_title" android:icon="@drawable/ic_settings_bluetooth2" />
<!-- Data Usage -->
<header android:id="@+id/data_usage_settings" android:fragment="com.android.settings.DataUsageSummary" android:title="@string/data_usage_summary_title" android:icon="@drawable/ic_settings_data_usage" />
<!-- Operator hook -->
<header android:fragment="com.android.settings.WirelessSettings" android:id="@+id/operator_settings">
<intent android:action="com.android.settings.OPERATOR_APPLICATION_SETTING" />
</header>
<!-- Other wireless and network controls -->
<header android:id="@+id/wireless_settings" android:title="@string/radio_controls_title" android:breadCrumbTitle="@string/wireless_networks_settings_title" android:fragment="com.android.settings.WirelessSettings" android:icon="@drawable/empty_icon" />
<!-- DEVICE -->
<header android:id="@+id/device_section" android:title="@string/header_category_device" />
<!-- Home -->
<header android:id="@+id/home_settings" android:icon="@drawable/ic_settings_home" android:fragment="com.android.settings.HomeSettings" android:title="@string/home_settings" />
<!-- Sound -->
<header android:id="@+id/sound_settings" android:icon="@drawable/ic_settings_sound" android:fragment="com.android.settings.SoundSettings" android:title="@string/sound_settings" />
<!-- Display -->
<header android:id="@+id/display_settings" android:icon="@drawable/ic_settings_display" android:fragment="com.android.settings.DisplaySettings" android:title="@string/display_settings" />
<!-- Storage -->
<header android:id="@+id/storage_settings" android:fragment="com.android.settings.deviceinfo.Memory" android:icon="@drawable/ic_settings_storage" android:title="@string/storage_settings" />
<!-- Battery -->
<header android:id="@+id/battery_settings" android:fragment="com.android.settings.fuelgauge.PowerUsageSummary" android:icon="@drawable/ic_settings_battery" android:title="@string/power_usage_summary_title" />
<!-- Application Settings -->
<header android:fragment="com.android.settings.applications.ManageApplications" android:icon="@drawable/ic_settings_applications" android:title="@string/applications_settings" android:id="@+id/application_settings" />
<!-- Manage users -->
<header android:fragment="com.android.settings.users.UserSettings" android:icon="@drawable/ic_settings_multiuser" android:title="@string/user_settings_title" android:id="@+id/user_settings" />
<!-- Manage NFC payment apps -->
<header android:fragment="com.android.settings.nfc.PaymentSettings" android:icon="@drawable/ic_settings_nfc_payment" android:title="@string/nfc_payment_settings_title" android:id="@+id/nfc_payment_settings" />
<!-- Manufacturer hook -->
<header android:fragment="com.android.settings.WirelessSettings" android:id="@+id/manufacturer_settings">
<intent android:action="com.android.settings.MANUFACTURER_APPLICATION_SETTING" />
</header>
<!-- PERSONAL -->
<header android:id="@+id/personal_section" android:title="@string/header_category_personal" />
<!-- Location -->
<header android:fragment="com.android.settings.location.LocationSettings" android:icon="@drawable/ic_settings_location" android:title="@string/location_settings_title" android:id="@+id/location_settings" />
<!-- Security -->
<header android:fragment="com.android.settings.SecuritySettings" android:icon="@drawable/ic_settings_security" android:title="@string/security_settings_title" android:id="@+id/security_settings" />
<!-- Language -->
<header android:id="@+id/language_settings" android:fragment="com.android.settings.inputmethod.InputMethodAndLanguageSettings" android:icon="@drawable/ic_settings_language" android:title="@string/language_settings" />
<!-- Backup and reset -->
<header android:fragment="com.android.settings.PrivacySettings" android:icon="@drawable/ic_settings_backup" android:title="@string/privacy_settings" android:id="@+id/privacy_settings" />
<!-- @Patch: GTV specific changes -->
<!-- MStar Android Patch Begin -->
<!-- Update -->
<header android:id="@+id/update_settings" android:fragment="com.android.settings.update.UpdateSettings" android:title="@string/update_settings" />
<!-- MStar Android Patch End -->
<!-- ACCOUNTS section -->
<header android:id="@+id/account_settings" android:title="@string/account_settings" />
<header android:id="@+id/account_add" android:title="@string/add_account_label" android:icon="@drawable/ic_menu_add_dark">
<intent android:action="android.settings.ADD_ACCOUNT_SETTINGS"/>
</header>
<!-- SYSTEM -->
<header android:id="@+id/system_section" android:title="@string/header_category_system" />
<!-- Date & Time -->
<header android:id="@+id/date_time_settings" android:fragment="com.android.settings.DateTimeSettings" android:icon="@drawable/ic_settings_date_time" android:title="@string/date_and_time_settings_title" />
<!-- Accessibility feedback -->
<header android:id="@+id/accessibility_settings" android:fragment="com.android.settings.accessibility.AccessibilitySettings" android:icon="@drawable/ic_settings_accessibility" android:title="@string/accessibility_settings" />
<!-- Print -->
<header android:id="@+id/print_settings" android:fragment="com.android.settings.print.PrintSettingsFragment" android:icon="@*android:drawable/ic_print" android:title="@string/print_settings" />
<!-- Development -->
<header android:id="@+id/development_settings" android:fragment="com.android.settings.DevelopmentSettings" android:icon="@drawable/ic_settings_development" android:title="@string/development_settings_title" />
<!-- About Device -->
<header android:id="@+id/about_settings" android:fragment="com.android.settings.DeviceInfoSettings" android:icon="@drawable/ic_settings_about" android:title="@string/about_settings" />
</preference-headers>
Ctrl+T查看下Settings的继承关系
Settings–>PreferenceActivity–>ListActivity–>Activity–>xxx
可以看到Settings是ListActivity的一个后代,可知Setting主界面的项都是放在一个ListView里面的,因为是ListView我们下面就来分析下他的点击事件。查看一下PreferenceActivity的源码,可以看到PreferenceActivity重写了ListActivity的onListItemClick:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (mAdapter != null) {
Object item = mAdapter.getItem(position);
if (item instanceof Header) onHeaderClick((Header) item, position);
}
}
再看onHeaderClick()方法:
public void onHeaderClick(Header header, int position) {
if (header.fragment != null) {
if (mSinglePane) {
int titleRes = header.breadCrumbTitleRes;
int shortTitleRes = header.breadCrumbShortTitleRes;
if (titleRes == 0) {
titleRes = header.titleRes;
shortTitleRes = 0;
}
startWithFragment(header.fragment, header.fragmentArguments, null, 0,
titleRes, shortTitleRes);
} else {
switchToHeader(header);
}
} else if (header.intent != null) {
startActivity(header.intent);
}
}
三、Settings模块优秀博文
由于Settings的具体模块有很多其他的优秀博文,我就不去分析了,不要重复造轮子。
菜鸟Android4.0 Settings分析(一):
http://blog.csdn.net/wu_shu_jun/article/details/8514465
菜鸟android4.0 Settings分析(二) 界面修改:
http://blog.csdn.net/wu_shu_jun/article/details/8662915
android settings源代码分析(1):
http://blog.csdn.net/zhudaozhuan/article/details/40618837
android settings源代码分析(2)- 分析“应用”模块:
http://blog.csdn.net/zhudaozhuan/article/details/40619371
android settings源代码分析(3)- “存储”模块:
http://blog.csdn.net/zhudaozhuan/article/details/40621335/
Android Settings开发之修改:
http://blog.csdn.net/wangjinyu501/article/details/22077803
Android4.0设置界面修改总结:
http://blog.csdn.net/way_ping_li/article/details/18076001