Android中webview的使用
一、问题:在顶部或者底部反复快速滑动界面,会出现自动向反方向滚动,或者视频有声音没有画面。
解决:在AndroidManifest.xml中给webview所在的activity添加硬件加速的属性:android:hardwareAccelerated="true"。
说明:1、view控件不能开启硬件加速(webView.setLayerType(View.LAYER_TYPE_HARDWARE,null);这个设置无效),但是view控件可以关闭该view的硬件加速:webView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
2、setLayerType方法只有在api>11才有。
3、参考网址:https://blog.csdn.net/u010142437/article/details/70915694
二、webview的设置
WebSettings webSettings=webView.getSettings();
webSettings.setUseWideViewPort(true);//设置WebView是应该启用对“viewport”HTML元标记的支持还是应该使用宽视口。true:支持<meta>标签的viewport属性
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
webSettings.setMediaPlaybackRequiresUserGesture(false);
}
webSettings.setBlockNetworkLoads(false);
webSettings.setAllowContentAccess(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webSettings.setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
webSettings.setDomStorageEnabled(true);//设置是否启用DOM存储API。默认false。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowUniversalAccessFromFileURLs(true);//可以解决有声音没图像得问题。设置是否允许在文件方案URL上下文中运行的JavaScript访问来自任何来源的内容。同上。
}
webSettings.setSupportZoom(true);
webSettings.setAllowFileAccess(true); //设置可以访问文件
webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口 webSettings.setLoadsImagesAutomatically(true); //支持自动加载webSettings.setPluginState(WebSettings.PluginState.ON);
说明:参考网址:https://blog.csdn.net/zhanwubus/article/details/80340025
三、webview长按下载图片。
webview.setOnLongClickListener(new OnLongClickLIstener(){
@Override
public boolean onLongClick(View view) {
int clickType=webview.getHitTestResult().getType();
if (clickType==WebView.HitTestResult.IMAGE_TYPE||clickType==WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE){
Log.d("webveiw","图片url="+wv.getHitTestResult().getExtra());
}
return false;
}
});
拓展:WebView.HitTestResult.EDIT_TEXT_TYPE:选中的文字类型;
WebView.HitTestResult.PHONE_TYPE:处理拨号;
WebView.HitTestResult.EMAIL_TYPE:处理Email;
WebView.HitTestResult.GEO_TYPE:地图类型;
WebView.HitTestResult.SRC_ANCHOR_TYPE:超链接;
WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:带有链接的图片类型;
WebView.HitTestResult.IMAGE_TYPE:图片类型;
WebView.HitTestResult.UNKNOWN_TYPE:未知类型。
private ValueCallback<Uri> mUploadMessage;
四、webview调用原生上传图片,视频,文件等。
全局变量:
private String acceptTypesStr;
private ValueCallback<Uri[]> uploadMessageAboveL;
private static final int FILE_CAMERA_RESULT_CODE = 129;
private File fileUpFile;
第一步:
webview.setWebChromeClient(new WebChromeClient(){
// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
showImgSeclect();
}
// For Android > 4.1.1
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
acceptTypesStr=acceptType;
mUploadMessage = uploadMsg;
Log.d("qtest",acceptTypesStr);
showImgSeclect();
}
// For Android > 5.0支持多张上传
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView,
ValueCallback<Uri[]> uploadMsg,
WebChromeClient.FileChooserParams fileChooserParams) {
acceptTypesStr="";
for (String str:fileChooserParams.getAcceptTypes()){
acceptTypesStr+=(TextUtils.isEmpty(acceptTypesStr)?"":";")+str;
}
Log.d("qtest",acceptTypesStr);
uploadMessageAboveL = uploadMsg;
showImgSeclect();
return true;
}
});
第二步:
/**
* 显示相机和相册选择
*/
public void showImgSeclect() {
//注意申请权限:Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE
if (TextUtils.isEmpty(acceptTypesStr))return;
Intent captureIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (acceptTypesStr.contains("image")){
captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUpFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()
+File.separator+"LinAn"+File.separator+"linan-"+System.currentTimeMillis()+".jpg");
}else if (acceptTypesStr.contains("video")){
captureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUpFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()
+File.separator+"LinAn"+File.separator+"linan-"+System.currentTimeMillis()+".mp4");
}
Uri mUri = null;
if (!fileUpFile.getParentFile().exists()) {
fileUpFile.getParentFile().mkdirs();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// 临时允许
captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
mUri = FileProvider.getUriForFile(getContext(), getPackageName() + ".fileprovider", fileUpFile);
} else {
mUri = Uri.fromFile(fileUpFile);
}
//需要显示应用的意图列表,这个list的顺序和选择菜单上的图标顺序是相关的,请注意。
List<Intent> cameraIntents = new ArrayList<Intent>();
PackageManager packageManager = getContext().getPackageManager();
//获取手机里所有注册相机接收意图的应用程序,放到意图列表里(无他相机,美颜相机等第三方相机)
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
String packageName = res.activityInfo.packageName;
Intent i = new Intent(captureIntent);
i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
i.setPackage(packageName);
i.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
cameraIntents.add(i);
}
//相册选择器
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(acceptTypesStr.contains("video")?"video/*":acceptTypesStr);
//intent选择器
Intent chooserIntent = Intent.createChooser(i, "choose");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
((CommonWeexActivity)getContext()).startActivityForResult(chooserIntent, FILE_CAMERA_RESULT_CODE);
}
第三步:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(getContext()).onActivityResult(requestCode, resultCode, data);//qq和新浪的回调
if (null == mUploadMessage && null == uploadMessageAboveL) {
return;
}
//没有返回值时的处理
if (resultCode != RESULT_OK) {
//需要回调onReceiveValue方法防止下次无法响应js方法
if (uploadMessageAboveL != null) {
uploadMessageAboveL.onReceiveValue(null);
uploadMessageAboveL = null;
}
if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(null);
mUploadMessage = null;
}
return;
}
Uri result = null;
if (requestCode == FILE_CAMERA_RESULT_CODE) {
if (null != data && null != data.getData()) {
result = data.getData();
}
if (result == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// 临时允许
result = FileProvider.getUriForFile(getContext(), getPackageName() + ".fileprovider", fileUpFile);
} else {
result = Uri.fromFile(fileUpFile);
}
}
//5.0以上设备的数据处理
if (uploadMessageAboveL != null) {
uploadMessageAboveL.onReceiveValue(new Uri[]{result});
uploadMessageAboveL = null;
} else if (mUploadMessage != null) {
//5.0以下设备的数据处理
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
}
五、webview中的video标签,视频全屏处理。
fl_video_full是该界面布局文件中的占满布局的一个FrameLayout控件。
ll_webview是该界面布局文件中占满布局,包含webview。
getHostView().setWebChromeClient(new WebChromeClient(){
@Override
public void onHideCustomView() {
try {
if (webview.fl_video_full==null)return;
webview.ll_webview.setVisibility(View.VISIBLE);
webview.fl_video_full.setVisibility(View.GONE);
webview.fl_video_full.removeAllViews();
webview.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);webview.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}catch (Exception e){
Log.d("qtest",e.toString());
}
super.onHideCustomView();
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
try{
videoFull=view;
webview.ll_webview.setVisibility(View.GONE);
webview.fl_video_full.setVisibility(View.VISIBLE);
webview.fl_video_full.addView(videoFull);
webview.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
webview.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); }catch (Exception e){
Log.d("qtest",e.toString());
}
super.onShowCustomView(view, callback);
}
});
六、销毁webveiw
if (webveiw!= null) {
webveiw.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
webveiw.clearHistory();
((ViewGroup)webveiw.getParent()).removeView(wv);
webveiw.destroy();
webveiw= null;
}
七、问题:webview加载https链接会出现空白页。
解决方法:
WebViewClient webViewClient = new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// super.onReceivedSslError(view, handler, error);
try {
handler.proceed();
}catch (Exception e){}
}
};
说明:ssl证书过期的话,更新证书需要注意除了服务器端,可能还需要更新DNS,即要更新好每个节点。