WebView can easily embed web pages into apps, and can directly call each other with js.
webview has two methods: setWebChromeClient and setWebClient
setWebClient: mainly deals with parsing, rendering web pages and other things that browsers do
setWebChromeClient: Assists WebView to handle Javascript dialogs, website icons, website titles, loading progress, etc.
WebViewClient is to help WebView handle various notifications and request events.
Set access network permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
Controls:
<WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" />
Use 1: Load local/Web resources
example.html is stored in the assets folder
Call WebView's loadUrl() method,
Load local resources
webView = (WebView) findViewById(R.id.webView); webView.loadUrl("file:///android_asset/example.html");
Load web resources:
webView = (WebView) findViewById(R.id.webView); webView.loadUrl("http://baidu.com");
Use 2: Open a web page in the program
Create your own WebViewClient and associate it with setWebViewClient
package com.example.testopen; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.test); init(); } private void init(){ webView = (WebView) findViewById(R.id.webView); //WebView loads web resources webView.loadUrl("http://baidu.com"); //Override WebView's default behavior of using a third-party or system default browser to open web pages, so that web pages are opened with WebView webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //When the return value is true, the control goes to the WebView to open, and if it is false, the system browser or a third-party browser is called view.loadUrl(url); return true; } }); } }
Use 3: If there is Javascript in the visited page, the webview must be set to support Javascript
//Enable support for javascript WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true);Use 4: If you want to browse the web page back instead of exiting the browser, you need WebView to cover the URL loading and let it automatically generate historical access records, so that you can access the visited sites by going forward or backward.
//Rewrite the physical button - the logic of the return @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode==KeyEvent.KEYCODE_BACK) { if(webView.canGoBack()) { webView.goBack();//Return to the previous page return true; } else { System.exit(0);//Exit the program } } return super.onKeyDown(keyCode, event); }Use 5: Judge the page loading process
webView.setWebChromeClient (new WebChromeClient () { @Override public void onProgressChanged(WebView view, int newProgress) { // TODO Auto-generated method stub if (newProgress == 100) { // page load is complete } else { // Loading } } });Purpose 6: The use of cache
Use cache first
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
Without caching:
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Example:
public class VideoViewActivity extends Activity implements View.OnClickListener { private RelativeLayout mLayoutTitle;//Title private TextView mTxtTitle;//Title text private ImageButton mBtnBack;//Back button private WebView mWebView;//Web page view private Intent mIntent; private String mUrl = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_video_view); mIntent = getIntent(); mUrl = mIntent.getExtras().getString("video_url"); //title mLayoutTitle = (RelativeLayout) findViewById(R.id.layout_title); mTxtTitle = (TextView) mLayoutTitle.findViewById(R.id.txt_title); mTxtTitle.setTextSize(18); mTxtTitle.setText(R.string.title_video_view); mBtnBack = (ImageButton) mLayoutTitle.findViewById(R.id.btn_back); mBtnBack.setOnClickListener(this); // mWebView = (WebView) findViewById(R.id.web_view); mWebView.setWebViewClient (new MyWebViewClient ()); mWebView.setWebChromeClient (new WebChromeClient () { @Override public void onReceivedTitle(WebView view, String title) { mTxtTitle.setText(title);// } }); WebSettings webSettings = mWebView.getSettings(); webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webSettings.setJavaScriptCanOpenWindowsAutomatically(true);//Set js to open the window directly, such as window.open(), the default is false webSettings.setSupportZoom(true);//Whether it can be zoomed, the default is true webSettings.setBuiltInZoomControls(true);//Whether to display the zoom button, the default is false webSettings.setUseWideViewPort(true);//Set this property, which can be scaled arbitrarily. Large view mode webSettings.setLoadWithOverviewMode(true);//Solve the problem of web page adaptation together with setUseWideViewPort(true) mWebView.loadUrl (mUrl); } class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // Rewrite this method to indicate that clicking on the link in the web page still jumps in the current webview, not the browser view.loadUrl(url); return true; } } }