Android接入WebView(一)——基本用法

在Android中,WebView是一种用来呈现网页内容的视图控件。与其他视图一样,它可以占据全屏也可以只占用activity的一部分。

(1)构造WebView

先在布局文件中定义一个WebView控件

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

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

(2)记得配置AndroidManifest.xml 网络权限:

<uses-permission android:name="android.permission.INTERNET"/>

(3)然后在activity中申明:

WebView webView=(WebView)findViewById(R.id.webview);
webView.loadUrl("https://blog.csdn.net/mountain_hua");

然后运行,发现并未显示网页,检查发现Android monitor里出现了

Denied starting an intent without a user gesture

意思是没有用户的主动手势,不能自动开始一个intent

先说解决方法:

方法一:给webVie设置如下:

webView.setWebViewClient(new WebViewClient());

方法二:或者给用户增加一个手势,比如通过点击Button打开这个网页

我用的方法一,再点击运行后,出现了在下的博客。webview使用成功。


那么为什么会出现

Denied starting an intent without a user gesture        

查询官方文档:

  1. From newest androids, the WebView and Chrome Client is separated application which can be automatically updated without user intention.

  2. From Chrome x >= 25 version, they changed how loading url is working in android application which is using webview component. https://developer.chrome.com/multidevice/android/intentsLooks like they are blocking changing url without user gesture and launched from JavaScript timers

Solution here is to force user to activate URL change, for example on button click.

Also, you can override method mentioned above "shouldOverrideUrlLoading" in WebView client.

第一条说最新的androids可以不用获得用户的intent,就能自动打开一个webview。

第二条说当Chrome版本大于等于25时,就需要用户的intent了。

解决方法就是我说的那两种:(1)通过Button打开。(2)重写shouldOverrideUrlLoading方法。即:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;                
    }
});

前进返回刷新:

package mountain_hua.learn_webview1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView webView=(WebView)findViewById(R.id.webview);
        webView.loadUrl("https://blog.csdn.net/mountain_hua");
        webView.setWebViewClient(new WebViewClient());
        Button b1=(Button)findViewById(R.id.button);
        Button b2=(Button)findViewById(R.id.button2);
        Button b3=(Button)findViewById(R.id.button3);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回
                webView.goBack();
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重新加载
                webView.reload();
            }
        });
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //前进
                webView.goForward();
            }
        });


    }
}



附常用方法:(常用方法参考了https://blog.csdn.net/u012322710/article/details/52789818

WebSetting常用方法


setDomStorageEnabled             开启缓存
setAllowFileAccess                  启用或禁用WebView访问文件数据
setBlockNetworkImage          是否显示网络图像
setBuiltInZoomControls         设置是否支持缩放 
setCacheMode                       设置缓冲的模式
setDefaultFontSize                 设置默认的字体大小
setDefaultTextEncodingName 设置在解码时时候用的默认编码
setFixedFontFamily                 设置固定使用的字体
setJavaScriptEnabled                 设置是否支持Javascript
setLayoutAlgorithm                 设置布局方式
setLightTouchEnabled         设置用鼠标激活被选项
setSupportZoom                         设置是否支持变焦



WebViewClient常用方法


doUpdateVisitedHistory 更新历史记录
onFormResubmission 应用程序重新请求网页数据
onLoadResource        加载指定地址提供的资源
onPageFinished                网页加载完毕
onPageStarted                网页开始加载
onReceivedError                报告错误信息
onScaleChanged        当WebView发生改变
shouldOverrideUrlLoading 控制新的连接在当前WebView中打开

与javascript交互见:Android接入WebView(二)——与JavaScript交互

猜你喜欢

转载自blog.csdn.net/mountain_hua/article/details/80585214