记录一下webview的使用过程,webview上传图片,调用原生方法

先记录一下我用webview时候setting的一些设置,这些设置我都是无脑粘贴:

WebSettings settings = webView.getSettings();
// 设置与Js交互的权限
settings.setJavaScriptEnabled(true);
// 设置允许JS弹窗
settings.setJavaScriptCanOpenWindowsAutomatically(true);
// 设置缩放级别
settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
// 支持缩放
settings.setSupportZoom(true);
settings.setDomStorageEnabled(true);//开启DOM storage API功能
settings.setAllowFileAccess(true);
// 将网页内容以单列显示
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

然后后来遇到一个问题  就是加载的网页布局乱了,经过查资料需要设置这个

settings.setUseWideViewPort(false);//这里需要设置为true,才能让Webivew支持<meta>标签的viewport属性

接下来记录一下重写的Webview中的方法和作用

webView.setWebViewClient(new WebViewClient(){};

以下重写的是WebViewClient中的方法:

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
    super.onReceivedError(view, request, error);//错误提示,当网页断网加载失败我们可以做一些相关跳转,避免出现断网的不友好图示:
    LogUtils.LOG("ceshi","dsfa"+error,"断网");
    Intent intent=new Intent(MainActivity.this,KongActivity.class);
    startActivity(intent);
    finish();
}
webView.setWebChromeClient(new WebChromeClient(){}

以下是重写WebChromeClient中的方法

 @Override
            public void onProgressChanged(WebView view, int newProgress) {

                super.onProgressChanged(view, newProgress);
                if(newProgress==100){
                    mPrigressBer.setVisibility(View.GONE);//加载完网页进度条消失
                    LogUtils.LOG("ceshi",webView.getUrl(),"网址");
                    uuuu=webView.getUrl();//可以得到加载的网页,我们可以判断当加载的网页是主页时清除掉历史,有效防止循环后退
                    if(webView.getUrl().contains("https://www.baidu.com")
                            ){
                        webView.clearHistory();//清楚webview加载的历史,点击返回直接退出
                    }
                }
                else{
                    mPrigressBer.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    mPrigressBer.setProgress(newProgress);//设置进度值
                }


            }

我的项目里还有一个需求上传图片,所以我在WebChromeClient中调用了这几个方法

public final static int FILECHOOSER_RESULTCODE = 1;
public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
public ValueCallback<Uri> mUploadMessage;
private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {
    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");
    startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}

public ValueCallback<Uri[]> mUploadMessageForAndroid5;
private void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) {
    mUploadMessageForAndroid5 = filePathCallback;
    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
    contentSelectionIntent.setType("image/*");

    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");

    startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
}

以及重写了

@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {

    LogUtils.LOG("ceshi","图片选择","tupian");
    onenFileChooseImpleForAndroid(filePathCallback);
    return true;

}

最后重写了onactivityresult方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage)
            return;
        Uri result = data == null || resultCode != RESULT_OK ? null: data.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;

    } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5){
        if (null == mUploadMessageForAndroid5)
            return;
        Uri result = (data == null || resultCode != RESULT_OK) ? null: data.getData();
        if (result != null) {
            mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
        } else {
            mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
        }
        mUploadMessageForAndroid5 = null;
    }
   
}

完整代码:

webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if(newProgress==100){
                    mPrigressBer.setVisibility(View.GONE);//加载完网页进度条消失
                    LogUtils.LOG("ceshi",webView.getUrl(),"网址");
                    uuuu=webView.getUrl();
                    if(webView.getUrl().contains("https://eosmytoken.io/app/index/index.html")||
                            webView.getUrl().contains("https://eosmytoken.io/app/news/index.html")||
                                    webView.getUrl().contains("https://eosmytoken.io/app/user/index.html")
//                    ||webView.getUrl().contains("http://app.ete-coin.com/app/user/index.html")
                            ){
                        webView.clearHistory();
                    }
                }
                else{
                    mPrigressBer.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    mPrigressBer.setProgress(newProgress);//设置进度值
                }


            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                LogUtils.LOG("ceshi",title,"网址网址网址");
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                    if(title.contains("找不到")){
                        Intent intent=new Intent(MainActivity.this,KongActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }

            }

            //扩展浏览器上传文件
            //3.0++版本
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                openFileChooserImpl(uploadMsg);
            }

            //3.0--版本
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                openFileChooserImpl(uploadMsg);
            }

            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                openFileChooserImpl(uploadMsg);
            }


            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {

                LogUtils.LOG("ceshi","图片选择","tupian");
                onenFileChooseImpleForAndroid(filePathCallback);
                return true;

            }

            @Override//这里是js交互。和web协商可以做调用原生操作  注意:重写这个方法后,web的alert不会弹出来
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Uri uri = Uri.parse(message);
                if ( uri.getScheme().equals("mytoken")) {

                    // 如果 authority  = 预先约定协议里的 webview,即代表都符合约定的协议
                    // 所以拦截url,下面JS开始调用Android需要的方法
                    Log.i("ceshi",uri.getAuthority()+".....2");
//                    webView.stopLoading();
                    if(uri.getAuthority().equals("scan")){
                       //做相关操作
                    }else {
                      //做相关操作
                    }

                    result.cancel();
                    return true;
                }
                return true;
            }
        });
public final static int FILECHOOSER_RESULTCODE = 1;
public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
public ValueCallback<Uri> mUploadMessage;
private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {
    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");
    startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}

public ValueCallback<Uri[]> mUploadMessageForAndroid5;
private void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) {
    mUploadMessageForAndroid5 = filePathCallback;
    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
    contentSelectionIntent.setType("image/*");

    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");

    startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage)
            return;
        Uri result = data == null || resultCode != RESULT_OK ? null: data.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;

    } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5){
        if (null == mUploadMessageForAndroid5)
            return;
        Uri result = (data == null || resultCode != RESULT_OK) ? null: data.getData();
        if (result != null) {
            mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
        } else {
            mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
        }
        mUploadMessageForAndroid5 = null;
    }}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值