一直以为webView就是加载个链接如此简单 原来webView中需要处理的问题也有很多
下面我们来看下webView中各种遇到的问题和解决方法
如果webView调用支付失败 可以将url拦截 对url做出判断
重新打开rul 如下代码:
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//对url做出判断
if (url.startsWith("http:") || url.startsWith("https:")) {
return false;
}
try {
//重新加载url
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} catch (Exception e) {
Log.e(TAG, " Exception is ==== >>> " + e);
}
return true;
}
});
// 不同方式的请求码
public static final int REQUEST_SELECT_FILE = 100;
public final static int FILECHOOSER_RESULTCODE = 1;
// 接收安卓5.0以上的
public ValueCallback uploadMessage;
//接收5.0以下的
private ValueCallback mUploadMessage;
//设置支持调用相册
mWebView.setWebChromeClient(new WebChromeClient() {
// Android 3.0+
public void openFileChooser(ValueCallback 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);
}
// Android 3.0+
public void openFileChooser(ValueCallback uploadMsg,String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "File Browser"),
ProxyActivity.FILECHOOSER_RESULTCODE);
}
// Android 4.1+ 会调用这个
public void openFileChooser(ValueCallback uploadMsg,String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(
Intent.createChooser(i, "File Chooser"),ProxyActivity.FILECHOOSER_RESULTCODE);
}
//Android 5.0会调用这个
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView,
ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
//确保没有现有的消息
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
startActivityForResult(intent, REQUEST_SELECT_FILE);
return true;
}
});
//将选中的图片结果带回
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == FILECHOOSER_RESULTCODE && resultCode == RESULT_OK) {
Uri uri = intent.getData();
Log.e("tag", "图片数据:" + uri.toString());
//将图片显示到webView
mUploadMessage.onReceiveValue(intent.getData());
mUploadMessage = null;
} else if (requestCode == REQUEST_SELECT_FILE) {
if (uploadMessage == null)
return;
uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
uploadMessage = null;
}
}
@Override
protected void onResume() {
super.onResume();
mWebView.onResume();
if (mUploadMessage != null) {
//取消之后要告诉WebView不要再等待返回结果,
设置为空就等于重置了状态,也是避免只能选择一次图片的原因
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
}
}
//解决webview加载的网页上的按钮点击失效 以及有些图标显示不出来的问题
WebSettings webSettings = mWebView.getSettings();
//下面这2句代码是关键 解决按钮点击失效
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
//点击回退按钮不是退出应用程序,而是返回上一个页面
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
if (mWebView != null) {
final ViewGroup viewGroup = (ViewGroup) mWebView.getParent();
if (viewGroup != null) {
viewGroup.removeView(mWebView);
}
mWebView.destroy();
}
super.onDestroy();
mUnbinder.unbind();
}
WebView实用性很强 需要熟悉各种用法 还是得多多练习
WebView是android中一个非常重要的控件,它的作用是用来展示一个web页面。
它使用的内核是webkit引擎,4.4版本之后,直接使用Chrome作为内置网页浏览器。
了解更多请参考简书 — WebView全面解析
我要一步一步往上爬
在最高点乘着叶片往前飞
任风吹干流过的泪和汗
我要一步一步往上爬
等待阳光静静看着它的脸
小小的天有大大的梦想
我有属于我的天
任风吹干流过的泪和汗
总有一天我有属于我的天