一骤坐、概述
本文主要對android M源碼中的設(shè)置進行分析妨马,此篇文章主要對第一次進入設(shè)置的過程進行分析进每。
二创橄、入口
AndroidManifest.xml中的<intent-filter> 定義apk默認入口如果有很多就默認第一個,設(shè)置入口Settings.java
<activity android:name="Settings"
android:taskAffinity="com.android.settings"
...
<intent-filter android:priority="1">
<action android:name="android.settings.SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
android:value="true" />
Settings.java中只定義了一些靜態(tài)內(nèi)部類虫腋,接著看Settings的繼承 SettingsActivity骄酗,SettingsActivity繼承自Activity那么我們就可以從SettingsActivity的onCreate函數(shù)對這個SettingsActivity進行分析了。
三岔乔、SettingsActivity
onCreate()
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
...
getMetaData();
final Intent intent = getIntent();
//...
final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
//...
mIsShowingDashboard = className.equals(Settings.class.getName());
setContentView(mIsShowingDashboard ?
R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
mContent = (ViewGroup) findViewById(R.id.main_content);
getFragmentManager().addOnBackStackChangedListener(this);
//...
if (savedState != null) {
//...
} else {
if (!mIsShowingDashboard) {
...
Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
switchToFragment(initialFragmentName, initialArguments, true, false,
mInitialTitleResId, mInitialTitle, false);
} else {
...
switchToFragment(DashboardSummary.class.getName(), null, false, false,
mInitialTitleResId, mInitialTitle, false);
}
}
//...
}
Class判斷
第一個函數(shù)getMetaData酥筝;獲得AndroidManifest中activyt的meta-data滚躯,給mFragmentClass給個值在getIntent中g(shù)etStartingFragmentClass雏门,可以看出此函數(shù)作用是打開的界面是屬于Settings的話就直接返回這個Class
private void getMetaData() {
try {
ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
PackageManager.GET_META_DATA);
if (ai == null || ai.metaData == null) return;
mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
} catch (NameNotFoundException nnfe) {
// No recovery
Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
}
}
布局
接下來布局設(shè)置,mIsShowingDashboard = true掸掏,所以是settings_main_dashboard茁影,打開可以看到里邊的布局里邊只有一個FrameLayout,所以可以看出主界面就是一個FrameLayout的容器
狀態(tài)
addOnBackStackChangedListener為back stack加上監(jiān)聽第一次進入savedState為空丧凤,savedState作用是對界面狀態(tài)的保存在SettingsAcitvity中的onSaveInstanceState對狀態(tài)保存一些操作募闲,這里就不詳述了。
載入
接著走到了switchToFragment(DashboardSummary.class.getName()....switchToFragment的作用就是加載一個fragment愿待。
private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
if (validate && !isValidFragment(fragmentName)) {
throw new IllegalArgumentException("Invalid fragment for this activity: "
+ fragmentName);
}
Fragment f = Fragment.instantiate(this, fragmentName, args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main_content, f);
if (withTransition) {
TransitionManager.beginDelayedTransition(mContent);
}
if (addToBackStack) {
transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
}
if (titleResId > 0) {
transaction.setBreadCrumbTitle(titleResId);
} else if (title != null) {
transaction.setBreadCrumbTitle(title);
}
transaction.commitAllowingStateLoss();
getFragmentManager().executePendingTransactions();
return f;
}
在SettingsActivity中浩螺,onCreate基本就走完了,onResume只是一些廣播的注冊數(shù)據(jù)的監(jiān)聽仍侥,接下來就看看這個載入DashboardSummary.java
四要出、DashboardSummary
此類主要管理主界面的菜單解析,加載整個創(chuàng)建過程代碼很少农渊,onCreateView中看到患蹂,布局是一個ViewGroup,在onResume中調(diào)用sendRebuildUI方法開始加載界面
private void sendRebuildUI() {
if (!mHandler.hasMessages(MSG_REBUILD_UI)) {
mHandler.sendEmptyMessage(MSG_REBUILD_UI);
}
}
接著在mHandler中調(diào)用rebuildUI
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_REBUILD_UI: {
final Context context = getActivity();
rebuildUI(context);
} break;
}
}
};
然后看最后的加載函數(shù)rebuildUI
private void rebuildUI(Context context) {
...
List<DashboardCategory> categories =
((SettingsActivity) context).getDashboardCategories(true);
final int count = categories.size();
for (int n = 0; n < count; n++) {
...
mDashboard.addView(categoryView);
}
...
}
里邊最關(guān)鍵的是調(diào)用了SettingsActivity的getDashboardCategories方法而在getDashboardCategories主要調(diào)用buildDashboardCategories
private void buildDashboardCategories(List<DashboardCategory> categories) {
categories.clear();
loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
updateTilesList(categories);
}
最后砸紊,我們打開dashboard_categories這個xml文件就可以到設(shè)置菜單的一些配置了
<dashboard-categories
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- WIRELESS and NETWORKS -->
<dashboard-category
android:id="@+id/wireless_section"
android:key="@string/category_key_wireless"
android:title="@string/header_category_wireless_networks" >
<!-- Wifi -->
<dashboard-tile
android:id="@+id/wifi_settings"
android:title="@string/wifi_settings_title"
android:fragment="com.android.settings.wifi.WifiSettings"
android:icon="@drawable/ic_settings_wireless"
/>
<!-- Bluetooth -->
<dashboard-tile
android:id="@+id/bluetooth_settings"
android:title="@string/bluetooth_settings_title"
android:fragment="com.android.settings.bluetooth.BluetoothSettings"
android:icon="@drawable/ic_settings_bluetooth"
/>
五传于、總結(jié)
設(shè)置進入后的初始化流程主要是通過SettingsActvity來加載DashboardSummary
最后再DashboardSummary進行初始化解析加載出界面。