美文网首页
Android WebView

Android WebView

作者: 百里漫步 | 来源:发表于2017-10-19 17:18 被阅读0次

    关于WebView加载网页的注意:
    1、覆盖webView使用本地系统浏览器或者第三方
    2、物理返回键返回网页
    3、加载网页进度条
    4、网页加载优化,优先使用缓存
    5、请事先注册用户Internet权限
    下面给出代码(注释详情):
    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >   
    <WebView 
        android:id="@+id/webView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    </LinearLayout>
    
    

    MainActivity.java

    package com.example.day101903;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    public class MainActivity extends Activity {
    
        private ProgressDialog progressDialog;
        private WebView webView;
        private String url = "http://www.baidu.com";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
              //跳转网页
    //        Uri uri = Uri.parse(url);
    //        Intent intent = new Intent(Intent.ACTION_VIEW,uri);
    //        startActivity(intent);
            
            webView = (WebView) findViewById(R.id.webView);
            webView.loadUrl("http://www.baidu.com");
            //覆盖webView使用浏览器打开网页
            webView.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    //返回值true为webview打开,false为第三方打开
                    view.loadUrl(url);
                    return true;
                }
            });
            //启用JavaScript
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
           //webView优先使用缓存
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            
            //监听网页加载进度
            webView.setWebChromeClient(new WebChromeClient(){
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    
                //newProgress为0-100的整数
                if(newProgress==100){
                    //网页加载完毕
                    closeDialog();
                }else{
                    //网页正在加载
                    openDialog(newProgress);
                }
                }
    
                private void openDialog(int newProgress) {
                    if(progressDialog==null){
                        progressDialog = new ProgressDialog(MainActivity.this);
                        progressDialog.setTitle("正在加载");
                        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        progressDialog.setProgress(newProgress);
                        progressDialog.show();
                    }else{
                        progressDialog.setProgress(newProgress);
                    }
                    
                }
    
                private void closeDialog() {
                    if(progressDialog!=null&&progressDialog.isShowing()){
                        progressDialog.dismiss();
                        progressDialog=null;
                    }
                    
                }
            });
            
            
        }
        
        //改写物理键返回的逻辑
        
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(keyCode==KeyEvent.KEYCODE_BACK){
                if(webView.canGoBack()){
                    webView.goBack();
                    return true;
                }
                else{
                    System.exit(0);
                }
            }
                
            
            return super.onKeyDown(keyCode, event);
        }
       
    }
    

    演示效果(截图时候不知道为什么进不去了,就这样吧~~):

    url.gif

    相关文章

      网友评论

          本文标题:Android WebView

          本文链接:https://www.haomeiwen.com/subject/rljuuxtx.html