Android WebView最佳化,androidwebview最佳化

來源:互聯網
上載者:User

Android WebView最佳化,androidwebview最佳化


WebView可以很好地協助我們展示html頁面,但是webview使用不當的話還是可能產生一定問題的,下面就以下幾個方面說說我的最佳化技巧

1、展示webview的activity可以另開一個進程,這樣就能和我們app的主進程分開了,即使webview產生了oom崩潰等問題也不會影響到主程式,如何?呢,其實很簡單,在androidmanifest.xml的activity標籤裡加上android:process="packagename.web"就可以了,代碼如下:

        <activity            android:name=".WebViewActivity"            android:process="com.web"            android:label="@string/app_name" >        </activity>

運行起來就會發現多了一個進程,哈哈


2、webview的建立也是有技巧的,最好不要在layout.xml中使用webview,可以通過一個viewgroup容器,使用代碼動態往容器裡addview(webview),這樣可以在onDestory()裡銷毀掉webview及時清理記憶體,另外需要注意建立webview需要使用applicationContext而不是activity的context,銷毀時不再佔有activity對象,這個大家應該都知道了,最後離開的時候需要及時銷毀webview,onDestory()中應該先從viewgroup中remove掉webview,再調用webview.removeAllViews();webview.destory();  代碼如下:

建立:

                ll = new LinearLayout(getApplicationContext()); ll.setOrientation(LinearLayout.VERTICAL);wv = new WebView(getApplicationContext());

銷毀:

        @Overrideprotected void onDestroy() {ll.removeAllViews();wv.stopLoading();wv.removeAllViews();wv.destroy();wv = null;ll = null;super.onDestroy();}

3、進一步的最佳化,activity被動被殺之後,最好能夠儲存webview狀態,這樣使用者下次開啟時就看到之前的狀態了,嗯,就這麼幹,webview支援saveState(bundle)和restoreState(bundle)方法,所以就簡單了,哈哈,看看代碼吧:

儲存狀態:

        @Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);wv.saveState(outState);Log.e(TAG, "save state...");}

是不是很熟悉啊,哈哈


恢複狀態:

在activity的onCreate(bundle savedInstanceState)裡,這麼吊用:

                if(null!=savedInstanceState){wv.restoreState(savedInstanceState);Log.i(TAG, "restore state");}else{wv.loadUrl("http://3g.cn");}

試用一下,結果還是令人滿意的,恢複的相當到位啊!32個贊!!!

但是,我還是有一個疑問,朋友圈開啟網頁back之後再開啟也能恢複到原來位置,不知道他們怎麼做到的,正常back的話是不會調用onSaveInstanceState(bundle)的,我們正常就沒法儲存狀態了,希望知道的朋友可以回複我一下,謝謝。

好了,先寫這麼多。


轉載請標明文章出處 http://blog.csdn.net/fkingu007/article/details/44650031,尊重原創。


下面貼出完整的代碼,希望對大家有個協助。


package com.example.test;


import android.app.Activity;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.util.Log;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.LinearLayout;


public class WebViewActivityextends Activity {

private staticfinal StringTAG = WebViewActivity.class.getName();

WebView wv;

LinearLayout ll;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

ll = new LinearLayout(getApplicationContext()); 

ll.setOrientation(LinearLayout.VERTICAL);

wv = new WebView(getApplicationContext());

WebSettings setting = wv.getSettings();

setting.setJavaScriptEnabled(true);

setting.setBuiltInZoomControls(true);

setting.setAppCacheEnabled(true);

setting.setDisplayZoomControls(true);

ll.addView(wv);

setContentView(ll);

if(null!=savedInstanceState){

wv.restoreState(savedInstanceState);

Log.i(TAG,"restore state");

}else{

wv.loadUrl("http://3g.cn");

}

wv.setWebViewClient(new WebViewClient(){


@Override

publicboolean shouldOverrideUrlLoading(WebView view, String url) {

Log.d(TAG,"jump to :"+url);

wv.loadUrl(url);

returntrue;

}


@Override

publicvoid onPageStarted(WebView view, String url, Bitmap favicon) {

super.onPageStarted(view, url, favicon);

Log.d(TAG,"pageStarted:"+url);

}


@Override

publicvoid onPageFinished(WebView view, String url) {

//TODO Auto-generated method stub

super.onPageFinished(view, url);

Log.d(TAG,"pageFinished:"+url);

}


@Override

publicvoid onReceivedError(WebView view,int errorCode,

String description, String failingUrl) {

//TODO Auto-generated method stub

super.onReceivedError(view, errorCode, description, failingUrl);

Log.e(TAG,"onReceivedError code :"+errorCode+" , failingUrl:"+failingUrl);

}

});

}

@Override

protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

wv.saveState(outState);

Log.e(TAG,"save state...");

}

@Override

public void onBackPressed() {

if(wv.canGoBack())

wv.goBack();

else{

super.onBackPressed();

// Process.killProcess(Process.myPid());

}

}

@Override

protected void onDestroy() {

ll.removeAllViews();

wv.stopLoading();

wv.removeAllViews();

wv.destroy();

wv = null;

ll = null;

super.onDestroy();

}


}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.