Android中的Context

來源:互聯網
上載者:User

  

  

 

   ContextWrapper      Context mbase;                    (mBase!=               IllegalStateException("Base context already set"          mbase=                         mBase.startActivity(intent);  }

  

    

               Application app=data.info.makeApplication(data.restrictedBackupMode,    Application makeApplication(               java.lang.ClassLoader cl=         ContextImpl appContext= ContextImpl();         appContext.init(,,mActivityThread);                  app= appContext.setOuterContext(app);  ....

  2.建立Activity對象的時機

    通過startActivity()或startActivityForResult()請求啟動一個Activity時,如果系統檢測需要建立一個Activity對象時,就會回調handleLaunchActivity()方法,該方法繼而調用performLaunchActivity()方法,去建立一個Activity執行個體,並且回調onCreate(),onStart()方法等,函數都位於ActivityThread.java類

                      Activity a = performLaunchActivity(r, customIntent);                   Activity activity =                                    java.lang.ClassLoader cl =             activity =           (activity !=              ContextImpl appContext =  ContextImpl();                   appContext.init(r.packageInfo, r.token, );                appContext.setOuterContext(activity);                    }  

  3.建立Service對象的時機

    同過startService或bindService時,如果系統檢測到需要新建立一個Service執行個體,就會回調handleCreateService()方法,完成相關資料操作。handleCreateService()函數位於ActivityThread.java類。

                               Service service =                       java.lang.ClassLoader cl =             service =         }            ContextImpl context =  ContextImpl();          context.init(packageInfo, , );                     Application app = packageInfo.makeApplication(                }  

  通過對ContextImpl的分析可知,其方法的大多數操作都是直接調用其屬性mPackageInfo(該屬性類型為PackageInfo)的相關方法而來。這說明ContextImpl是一種輕量級的類,而PackageInfo才是真正重量級的類。而一個App裡的所有ContextImpl執行個體,都對應同一個packageInfo對象。

  最後分析利用Context擷取SharedPreferences類,一般的擷取方法就是通過getSharedPreference()方法根據相關資訊擷取SharedPreference對象。

具體如下:

  1.調用getSharedPreferences()擷取對應的檔案,該函數實現功能如下:

             HashMap<File, SharedPreferencesImpl> sSharedPrefs =                HashMap<File, SharedPreferencesImpl>              SharedPreferences getSharedPreferences(String name,                      File f = getSharedPrefsFile(name);             (sSharedPrefs) {                     sp =               (sp !=  && !                                                          Map map =            (f.exists() &&                                str =                   map =                                         (sp !=                                     sp.replace(map);                 }                   sp =                   (sp ==                                             sp =                        }  

  2.SharedPreferences是個介面,它定義了一些操作xml檔案的方法,其真正實作類別為SharedPreferencesImpl,該類是ContextImpl的內部類,該類如下:

                   SharedPreferencesImpl            Map mMap;                                                 (                  String v =                   v !=  ?                          EditorImpl                 Map<String, Object> mModified = Maps.newHashMap();       }  

  以上內容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.