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

当我们在Launcher中点击浏览器的图标时,浏览器的窗口会打开并显示主页(HomePage)。这里我们对这一场景进行分析,研究浏览器如何启动,取得缺省主页并将它布局和显示的。

根据前边对WebView 类的学习,大概可以预期我们在主Activity的onCreate方法里从设置里面取得缺省主页的配置,创建一个WebView类,并使用setContentView将它添加到主窗口中。下面我们从浏览器的代码看看它是如何实现的。

首先,研究AndroidManifest文件,从标签的内容看到该Apk实现了自己的Applicaton 类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:

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" >

。。。

Apk的启动,首先是ApplicationBrowser类的onCreate方法,主要工作:

// create CookieSyncManager with current Context

CookieSyncManager.createInstance(this);

BrowserSettings.initialize(getApplicationContext());

Preloader.initialize(getApplicationContext());

这里涉及到三个工作:Cookie同步管理,浏览器设置和预加载。

然后是Activity的onCreate方法,与我们的研究相关的代码:

@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的创建和Controller的start。

Controller的构造方法主要涉及到以下几个相关类:

BrowserSettings

TabControl

CrashRecoveryHandler

UrlHandler

BrowserWebViewFactory

IntentHandler

PageDialogHandler

BookMarks的ContentObserver

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_screen的layout文件:

xmlns:android="http://schemas.android.com/apk/res/android">

android:visibility="gone"

android:background="@color/black"

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

android:orientation="vertical"

android:id="@+id/vertical_layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

可以看出这个是浏览器主界面的布局,浏览器的布局已经准备好,后面我们创建的WebView应该是添加到main_content里面。

Controller的start方法执行了CrashRecoveryHandler的startRecovery().

CrashRecoveryHandler相关操作:

首先是initialize(),创建了CrashRecoveryHandler实例,CrashRecoveryHandler实例构造了foregroundHandler和backgroundHandler。

CrashRecoveryHandler的preloadCrashState方法,在backgroundHandler的处理中执行loadCrashState(),该方法将CrashState从STATE_FILE读入到mRecoveryState中。

CrashRecoveryHandler的startRecovery方法,调用Controller的doStart()。

Controller的doStart方法调用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.浏览器的主Activity是BrowserActivity,在其onCreate方法中构建了Controller和PhoneUi,并调用Controller::start方法启动Controller;

a)Controller在其构造方法中实例化和初始化一些协助对象,其中一个重要的类是CrashRecoveryHandler;

b)PhoneUi的构造方法加载custom_screen布局文件,并将它作为Activity窗口的ContentView.

c)Controller::start方法执行了CrashRecoveryHandler的startRecovery(),该方法又调用Controller的doStart()方法

i.Controller的doStart方法调用onPreloginFinished(),该方法执行openTabToHomePage,打开浏览器主页。具体将WebView加到ContentView的方法是BaseUi的attachTabToContentView方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值