當(dāng)我們?cè)贚auncher中點(diǎn)擊瀏覽器的圖標(biāo)時(shí),瀏覽器的窗口會(huì)打開并顯示主頁(HomePage)。這里我們對(duì)這一場(chǎng)景進(jìn)行分析,研究瀏覽器如何啟動(dòng),取得缺省主頁并將它布局和顯示的。
成都創(chuàng)新互聯(lián)公司主營(yíng)西華網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app軟件開發(fā),西華h5微信小程序開發(fā)搭建,西華網(wǎng)站營(yíng)銷推廣歡迎西華等地區(qū)企業(yè)咨詢
根據(jù)前邊對(duì)WebView 類的學(xué)習(xí),大概可以預(yù)期我們?cè)谥鰽ctivity的onCreate方法里從設(shè)置里面取得缺省主頁的配置,創(chuàng)建一個(gè)WebView類,并使用setContentView將它添加到主窗口中。下面我們從瀏覽器的代碼看看它是如何實(shí)現(xiàn)的。
首先,研究AndroidManifest文件,從<application>標(biāo)簽的內(nèi)容看到該Apk實(shí)現(xiàn)了自己的Applicaton 類Browser:
<application android:name="Browser" android:label="@string/application_name" android:icon="@mipmap/ic_launcher_browser" android:backupAgent=".BrowserBackupAgent" android:hardwareAccelerated="true" android:taskAffinity="android.task.browser" >另外,該Apk的主Activity為BrowserActivity:
<activity android:name="BrowserActivity" android:label="@string/application_name" android:launchMode="singleTask" android:alwaysRetainTaskState="true" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:theme="@style/BrowserTheme" android:windowSoftInputMode="adjustResize" >
。。。
<!-- We are also the main entry point of the browser. --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.APP_BROWSER" /> </intent-filter>
Apk的啟動(dòng),首先是ApplicationBrowser類的onCreate方法,主要工作:
// create CookieSyncManager with current Context
CookieSyncManager.createInstance(this);
BrowserSettings.initialize(getApplicationContext());
Preloader.initialize(getApplicationContext());
這里涉及到三個(gè)工作:Cookie同步管理,瀏覽器設(shè)置和預(yù)加載。
然后是Activity的onCreate方法,與我們的研究相關(guān)的代碼:
@Override public void onCreate(Bundle icicle) { if (LOGV_ENABLED) { Log.v(LOGTAG, this + " onStart, has state: " + (icicle == null ? "false" : "true")); } super.onCreate(icicle); mController = createController(); Intent intent = (icicle == null) ? getIntent() : null; mController.start(intent); }createController方法:
private Controller createController() { Controller controller = new Controller(this); boolean xlarge = isTablet(this); UI ui = null; if (xlarge) { ui = new XLargeUi(this, controller); } else { ui = new PhoneUi(this, controller); } controller.setUi(ui); return controller; }
主要的工作是Controller的創(chuàng)建,PhoneUi的創(chuàng)建和Controller的start。
Controller的構(gòu)造方法主要涉及到以下幾個(gè)相關(guān)類:
BrowserSettings
TabControl
CrashRecoveryHandler
UrlHandler
BrowserWebViewFactory
IntentHandler
PageDialogHandler
BookMarks的ContentObserver
NetworkStateHandler
SystemAllowGeolocationOrigins
IconDataBase
PhoneUi的構(gòu)造:
BaseUi的構(gòu)造:
FrameLayout frameLayout = (FrameLayout) mActivity.getWindow() .getDecorView().findViewById(android.R.id.content); LayoutInflater.from(mActivity) .inflate(R.layout.custom_screen, frameLayout); mFixedTitlebarContainer = (FrameLayout) frameLayout.findViewById( R.id.fixed_titlebar_container); mContentView = (FrameLayout) frameLayout.findViewById( R.id.main_content); mCustomViewContainer = (FrameLayout) frameLayout.findViewById( R.id.fullscreen_custom_content); mErrorConsoleContainer = (LinearLayout) frameLayout .findViewById(R.id.error_console);
Custom_screen的layout文件:
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <FrameLayout android:id="@+id/fullscreen_custom_content" android:visibility="gone" android:background="@color/black" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.android.browser.view.CustomScreenLinearLayout android:orientation="vertical" android:id="@+id/vertical_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/error_console" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@+id/fixed_titlebar_container" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" /> </com.android.browser.view.CustomScreenLinearLayout> </merge>
可以看出這個(gè)是瀏覽器主界面的布局,瀏覽器的布局已經(jīng)準(zhǔn)備好,后面我們創(chuàng)建的WebView應(yīng)該是添加到main_content里面。
Controller的start方法執(zhí)行了CrashRecoveryHandler的startRecovery().
CrashRecoveryHandler相關(guān)操作:
首先是initialize(),創(chuàng)建了CrashRecoveryHandler實(shí)例,CrashRecoveryHandler實(shí)例構(gòu)造了foregroundHandler和backgroundHandler。
CrashRecoveryHandler的preloadCrashState方法,在backgroundHandler的處理中執(zhí)行l(wèi)oadCrashState(),該方法將CrashState從STATE_FILE讀入到mRecoveryState中。
CrashRecoveryHandler的startRecovery方法,調(diào)用Controller的doStart()。
Controller的doStart方法調(diào)用onPreloginFinished().
currentTabId is -1, thenopenTabToHomePage().
openTabToHomePage
createNewTabthen loadUrl.
createNewTab的實(shí)現(xiàn):
TabControl::createNewTab
createNewWebView:
new BrowserWebView;該類主要用來管理WebView滾動(dòng)條事件。
initWebViewSettings;
setActiveTab
TabControl::setCurrentTab
PhoneUi::setActiveTab
attachTabToContentView
至此,我們看完Apk啟動(dòng)并加載HomePage的過程,簡(jiǎn)單總結(jié)如下:
1.瀏覽器實(shí)現(xiàn)了自己的Application類(Browser),在其onCreate方法中進(jìn)行了一些初始化工作(Cookie同步管理,瀏覽器設(shè)置和預(yù)加載);
2.瀏覽器的主Activity是BrowserActivity,在其onCreate方法中構(gòu)建了Controller和PhoneUi,并調(diào)用Controller::start方法啟動(dòng)Controller;
a)Controller在其構(gòu)造方法中實(shí)例化和初始化一些協(xié)助對(duì)象,其中一個(gè)重要的類是CrashRecoveryHandler;
b)PhoneUi的構(gòu)造方法加載custom_screen布局文件,并將它作為Activity窗口的ContentView.
c)Controller::start方法執(zhí)行了CrashRecoveryHandler的startRecovery(),該方法又調(diào)用Controller的doStart()方法
i.Controller的doStart方法調(diào)用onPreloginFinished(),該方法執(zhí)行openTabToHomePage,打開瀏覽器主頁。具體將WebView加到ContentView的方法是BaseUi的attachTabToContentView方法。
本文標(biāo)題:Android瀏覽器的研究(四)---Apk的啟動(dòng)和主頁的加載過程
鏈接分享:http://muchs.cn/article12/jcpidc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、建站公司、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、外貿(mào)建站、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)