Android M 設置流程簡要分析

分類:編程 時間:2017-02-15

這份筆記主要是記錄設置的大致流程,以便在後續項目中需要修改設置的時候以便快熟理解大的思路.
首先從入口開始,也就是從第一個Activity開始,在androidManifest.xml裏面找到android.intent.category.LAUNCHER,這個activity-alias 就是要找的入口,其實這只是一個別名 真正想要看的是它的android:targetActivity="Settings",也就是<activity android:name="Settings" .
  1. <activity android:name="Settings"
  2. android:taskAffinity="com.android.settings"
  3. android:label="@string/settings_label_launcher"
  4. android:launchMode="singleTask">
  5. <intent-filter android:priority="1">
  6. <action android:name="android.settings.SETTINGS" />
  7. <category android:name="android.intent.category.DEFAULT" />
  8. </intent-filter>
  9. <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
  10. android:value="true" />
  11. </activity>
  12. <!-- Alias for launcher activity only, as this belongs to each profile. -->
  13. <activity-alias android:name="Settings"
  14. android:taskAffinity="com.android.settings"
  15. android:label="@string/settings_label_launcher"
  16. android:launchMode="singleTask"
  17. android:targetActivity="Settings">
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.DEFAULT" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity-alias>

打開這個Settings.Java發現裏面主要是定義了很多內部靜態Activity,主要邏輯還是在其父類SettingsActivity.java 裏面,那就從onCreate()開始
  1. @Override
  2. protected void onCreate(Bundle savedState) {
  3. super.onCreate(savedState);
  4. mExt = UtilsExt.getMiscPlugin(this);
  5. // Should happen before any call to getIntent()
  6. getMetaData();//獲取配置meta-data中的com.android.settings.FRAGMENT_CLASS屬性的值,將值保存在mFragmentClass裏面
  7. final Intent intent = getIntent();
  8. if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
  9. getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
  10. }
  11. mDevelopmentPreferences = getSharedPreferences(DevelopmentSettings.PREF_FILE,
  12. Context.MODE_PRIVATE);
  13. // Getting Intent properties can only be done after the super.onCreate(...)
  14. final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
  15. mIsShortcut = isShortCutIntent(intent) || isLikeShortCutIntent(intent) ||
  16. intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, false);
  17. final ComponentName cn = intent.getComponent();
  18. final String className = cn.getClassName();
  19. mIsShowingDashboard = className.equals(Settings.class.getName());
  20. // This is a "Sub Settings" when:
  21. // - this is a real SubSettings
  22. // - or :settings:show_fragment_as_subsetting is passed to the Intent
  23. final boolean isSubSettings = this instanceof SubSettings ||
  24. intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
  25. // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content insets
  26. if (isSubSettings) {
  27. // Check also that we are not a Theme Dialog as we don't want to override them
  28. final int themeResId = getThemeResId();
  29. if (themeResId != R.style.Theme_DialogWhenLarge &&
  30. themeResId != R.style.Theme_SubSettingsDialogWhenLarge) {
  31. setTheme(R.style.Theme_SubSettings);
  32. }
  33. }
  34. setContentView(mIsShowingDashboard ?
  35. R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
  36. mContent = (ViewGroup) findViewById(R.id.main_content);
  37. getFragmentManager().addOnBackStackChangedListener(this);
  38. if (mIsShowingDashboard) {
  39. // Run the Index update only if we have some space
  40. if (!Utils.isLowStorage(this)) {
  41. Index.getInstance(getApplicationContext()).update();
  42. } else {
  43. Log.w(LOG_TAG, "Cannot update the Indexer as we are running low on storage space!");
  44. }
  45. }
  46. if (savedState != null) {
  47. // We are restarting from a previous saved state; used that to initialize, instead
  48. // of starting fresh.
  49. mSearchMenuItemExpanded = savedState.getBoolean(SAVE_KEY_SEARCH_MENU_EXPANDED);
  50. mSearchQuery = savedState.getString(SAVE_KEY_SEARCH_QUERY);
  51. setTitleFromIntent(intent);
  52. ArrayList<DashboardCategory> categories =
  53. savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
  54. if (categories != null) {
  55. mCategories.clear();
  56. mCategories.addAll(categories);
  57. setTitleFromBackStack();
  58. }
  59. mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
  60. mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
  61. mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
  62. 1 /* one home activity by default */);

  63. Tags: Android android activity category profile

    文章來源:


ads
ads

相關文章
ads

相關文章

ad