Android Browser学习一 application的初始化

Android Browser 是一个非常好的学习资料, 使用了自己设计的MVC架构,来管理书签, 浏览器 等各个功能,有具有一定的稳定性,知道我们学习
 
Browser是从Application开始启动的,启动流程如下:

 

 

代码很简单:
01 public class Browser extends Application {
02  
03  
04     private final static String LOGTAG = "browser";
05      
06     // Set to true to enable verbose logging.
07     final static boolean LOGV_ENABLED = false;
08  
09  
10     // Set to true to enable extra debug logging.
11     final static boolean LOGD_ENABLED = true;
12  
13  
14     @Override
15     public void onCreate() {
16         super.onCreate();
17          if (LOGV_ENABLED)
18             Log.v(LOGTAG, "Browser.onCreate: this=" + this);
19  
20  
21         // create CookieSyncManager with current Context
22         //初始化cookie
23         CookieSyncManager.createInstance(this);
24         //在application 启动的时候实例化浏览器的配置BrowserSetting, 和 一下需要预先 载入的东西 Preloader等待以后调用
25         BrowserSettings.initialize(getApplicationContext());
26         Preloader.initialize(getApplicationContext());
27     }
28  
29  
30 }

 

 

主要功能是设置一些Debug的开关,初始化一下全局的模块,包括:CookieSyncManager, BrowserSettings ,PreLoader.

那么看一下他们分别是用来做什么的

CookieSyncManager: 初始化浏览器全局的cookie, 这是Android系统的类. 这个我们在做WebView开发的时候可能也用过.

Android通过使用这个类来实现Ram和硬盘中cookie的同步,提高了cookie的访问速度

具体使用参考 http://whao189.iteye.com/blog/1121865 

BrowserSettings :初始化浏览器的偏好设置, UA , WebView的设置 ,个人账户信息, 等

具体代码:

 

 

1 private BrowserSettings(Context context) {
2        mContext = context.getApplicationContext();
3        mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);//用来取xml的数据
4        mAutofillHandler = new AutofillHandler(mContext); //账户个人信息
5        mManagedSettings = new LinkedList<WeakReference<WebSettings>>();//WebView的设置
6        mCustomUserAgents = new WeakHashMap<WebSettings, String>(); //UA
7        mAutofillHandler.asyncLoadFromDb(); //异步载入个人信息
8        BackgroundHandler.execute(mSetup); //载入浏览器配置
9    }

有一个小点:根据dvm虚拟机的大小设置 内存缓存的网页数目 ,以及磁盘缓存

1 DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
2            mFontSizeMult = metrics.scaledDensity / metrics.density;
3            // the cost of one cached page is ~3M (measured using nytimes.com). For
4            // low end devices, we only cache one page. For high end devices, we try
5            // to cache more pages, currently choose 5.
6            //这是获取dvm的一个大体值 16m  24m 32m ...然后设置浏览器缓存多少网页
7            if (ActivityManager.staticGetMemoryClass() > 16) {
8                mPageCacheCapacity = 5;
9            }
1 //磁盘缓存空间
2             mWebStorageSizeManager = new WebStorageSizeManager(mContext,
3                     new WebStorageSizeManager.StatFsDiskInfo(getAppCachePath()),
4                     new WebStorageSizeManager.WebKitAppCacheInfo(getAppCachePath()));

 

 

 

将来BrowserSetting还会通过Controller和UI进行很多的交换,而且在Browser启动的时候是全局都要用到的,因此作出了全局唯一的单例

 

PreLoader: 如果Brower在后台,或者还在内存中,再次调用browser的时候就不会重新创建Webview等控件,提高启动的速度,主要是通过hold了一个含有webview的mSession,然后利用了application 部分内存不会轻易释放 来实现的. 具体后面会继续分析,

1 private Preloader(Context context) {
2       mContext = context.getApplicationContext();
3       mHandler = new Handler(Looper.getMainLooper());
4       mSession = null;
5       mFactory = new BrowserWebViewFactory(context);
6  
7   }

这样Application的启动就ok了

可以看到, 作者把一些App全局需要用到的东西放到了Application中初始化, 并且这些东西都是单例的,这一在app周期中他们将一直存在, 后面的组件只要调用getInstance就可以使用了.不过这里有个问题, 这些单例会占用app的内存直到app退出,所以全局初始化的东西应该尽量少一些.

posted @ 2015-08-07 14:43  统领七界  阅读(648)  评论(0编辑  收藏  举报