日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 浏览器的研究(四)--- Apk的启动和主页的加载过程

發布時間:2025/5/22 Android 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 浏览器的研究(四)--- Apk的启动和主页的加载过程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

當我們在Launcher中點擊瀏覽器的圖標時,瀏覽器的窗口會打開并顯示主頁(HomePage)。這里我們對這一場景進行分析,研究瀏覽器如何啟動,取得缺省主頁并將它布局和顯示的。


根據前邊對WebView 類的學習,大概可以預期我們在主ActivityonCreate方法里從設置里面取得缺省主頁的配置,創建一個WebView類,并使用setContentView將它添加到主窗口中。下面我們從瀏覽器的代碼看看它是如何實現的。

首先,研究AndroidManifest文件,從<application>標簽的內容看到該Apk實現了自己的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的啟動,首先是ApplicationBrowser類的onCreate方法,主要工作:

? ?// create CookieSyncManager with current Context

? ?CookieSyncManager.createInstance(this);

? ?BrowserSettings.initialize(getApplicationContext());

? ?Preloader.initialize(getApplicationContext());

這里涉及到三個工作:Cookie同步管理,瀏覽器設置和預加載。

然后是ActivityonCreate方法,與我們的研究相關的代碼:

@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的創建,PhoneUi的創建和Controllerstart


Controller的構造方法主要涉及到以下幾個相關類:

BrowserSettings

TabControl

CrashRecoveryHandler

UrlHandler

BrowserWebViewFactory

IntentHandler

PageDialogHandler

BookMarksContentObserver

NetworkStateHandler

SystemAllowGeolocationOrigins

IconDataBase

PhoneUi的構造:

? ?BaseUi的構造:

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_screenlayout文件:

<mergexmlns: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.CustomScreenLinearLayoutandroid: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>


可以看出這個是瀏覽器主界面的布局,瀏覽器的布局已經準備好,后面我們創建的WebView應該是添加到main_content里面。


Controller start 方法執行了CrashRecoveryHandlerstartRecovery().

CrashRecoveryHandler相關操作:

首先是initialize(),創建了CrashRecoveryHandler實例,CrashRecoveryHandler實例構造了foregroundHandlerbackgroundHandler

CrashRecoveryHandlerpreloadCrashState方法,在backgroundHandler的處理中執行loadCrashState(),該方法將CrashStateSTATE_FILE讀入到mRecoveryState中。

CrashRecoveryHandlerstartRecovery方法,調用ControllerdoStart()

ControllerdoStart方法調用onPreloginFinished().

currentTabId is -1, thenopenTabToHomePage().

openTabToHomePage

createNewTabthen loadUrl.

createNewTab的實現:

TabControl::createNewTab

createNewWebView:

new BrowserWebView;該類主要用來管理WebView滾動條事件。

initWebViewSettings

setActiveTab

TabControl::setCurrentTab

PhoneUi::setActiveTab

attachTabToContentView

至此,我們看完Apk啟動并加載HomePage的過程,簡單總結如下:

1.瀏覽器實現了自己的Application類(Browser,在其onCreate方法中進行了一些初始化工作(Cookie同步管理,瀏覽器設置和預加載);

2.瀏覽器的主ActivityBrowserActivity,在其onCreate方法中構建了ControllerPhoneUi,并調用Controller::start方法啟動Controller

a)Controller在其構造方法中實例化和初始化一些協助對象,其中一個重要的類是CrashRecoveryHandler

b)PhoneUi的構造方法加載custom_screen布局文件,并將它作為Activity窗口的ContentView.

c)Controller::start方法執行了CrashRecoveryHandlerstartRecovery(),該方法又調用ControllerdoStart()方法

i.ControllerdoStart方法調用onPreloginFinished(),該方法執行openTabToHomePage,打開瀏覽器主頁。具體將WebView加到ContentView的方法是BaseUiattachTabToContentView方法。





轉載于:https://blog.51cto.com/sunhongbo/1357127

總結

以上是生活随笔為你收集整理的Android 浏览器的研究(四)--- Apk的启动和主页的加载过程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。