AndroidStudio
直接在main目录下创建jniLib,然后在jniLibs目录下创建armeabi文件夹,再把so放armeabi目录(main——>jniLibs——>armeabi)
在build.gradle(Module:app)添加如下代码 创建jniLibs
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
ndk { //选择要添加的对应cpu类型的.so库。 abiFilters 'armeabi' //, 'armeabi-v7a', 'armeabi-v8a', 'x86', 'x86_64', 'mips', 'mips64' // 还可以添加 }
把liblbs.so放在armeabi目录下
然后关联jar包。
在Application的onCreate()方法添加初始化腾讯内核浏览器
//搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。 QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() { @Override public void onViewInitFinished(boolean arg0) { Logutils.i("onViewInitFinished is " + arg0); } @Override public void onCoreInitFinished() { } }; QbSdk.setTbsListener(new TbsListener() { @Override public void onDownloadFinish(int i) { Logutils.i("onDownloadFinish"); } @Override public void onInstallFinish(int i) { Logutils.i("onInstallFinish"); } @Override public void onDownloadProgress(int i) { Logutils.i("onDownloadProgress:" + i); } }); QbSdk.initX5Environment(mContext, cb);
开始使用X5webview
<com.tencent.smtt.sdk.WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent">com.tencent.smtt.sdk.WebView>
初始化 webview设置
private void initwebview() { WebSettings seting = webview.getSettings(); seting.setAllowFileAccess(true); seting.setJavaScriptEnabled(true);//设置webview支持javascript脚本 // 设置可以支持缩放 seting.setSupportZoom(true); // 设置出现缩放工具 seting.setBuiltInZoomControls(true); webview.addJavascriptInterface(new JsOperation(this), "AndroidClient"); // //扩大比例的缩放 seting.setUseWideViewPort(true); webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("tel:")) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } else view.loadUrl(url); return true; } });
//调下载的方法 webview.setDownloadListener(new MyWebViewDownLoadListener());
//调拍照浏览器的方法
webview.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { // TODO 自动生成的方法存根 if (newProgress == 100) { pb.setVisibility(View.GONE);//加载完网页进度条消失 } else { pb.setVisibility(View.VISIBLE);//开始加载网页时显示进度条 pb.setProgress(newProgress);//设置进度值 } } //Android 5.0+ @Override public boolean onShowFileChooser(WebView webView, com.tencent.smtt.sdk.ValueCallbackvalueCallback, FileChooserParams fileChooserParams) { mUploadCallbackAboveL = valueCallback; takePhoto(); return true; } // Android < 3.0 调用这个方法 public void openFileChooser(ValueCallback uploadMsg) { mUploadMessage = uploadMsg; takePhoto(); } // 3.0 + 调用这个方法 public void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; takePhoto(); } // Android > 4.1.1 调用这个方法 public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; takePhoto(); } } ); /** * 调用loadUrl()方法进行加载内容 */ webview.loadUrl(url); }
private class MyWebViewDownLoadListener implements DownloadListener, com.tencent.smtt.sdk.DownloadListener { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage && null == mUploadCallbackAboveL) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); if (mUploadCallbackAboveL != null) { onActivityResultAboveL(requestCode, resultCode, data); } else if (mUploadMessage != null) { Log.e("Mr.Kang", "onActivityResult: " + result); if (result == null) { mUploadMessage.onReceiveValue(imageUri); mUploadMessage = null; Log.e("Mr.Kang", "onActivityResult: " + imageUri); } else { mUploadMessage.onReceiveValue(result); mUploadMessage = null; } } } } @SuppressWarnings("null") @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) { if (requestCode != FILECHOOSER_RESULTCODE || mUploadCallbackAboveL == null) { return; } Uri[] results = null; if (resultCode == Activity.RESULT_OK) { if (data == null) { results = new Uri[]{imageUri}; } else { String dataString = data.getDataString(); ClipData clipData = data.getClipData(); if (clipData != null) { results = new Uri[clipData.getItemCount()]; for (int i = 0; i < clipData.getItemCount(); i++) { ClipData.Item item = clipData.getItemAt(i); results[i] = item.getUri(); } } if (dataString != null) results = new Uri[]{Uri.parse(dataString)}; } } if (results != null) { mUploadCallbackAboveL.onReceiveValue(results); mUploadCallbackAboveL = null; } else { results = new Uri[]{imageUri}; mUploadCallbackAboveL.onReceiveValue(results); mUploadCallbackAboveL = null; } return; } private void takePhoto() { File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp"); // Create the storage directory if it does not exist if (!imageStorageDir.exists()) { imageStorageDir.mkdirs(); } File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); imageUri = Uri.fromFile(file); final ListcameraIntents = new ArrayList (); final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = getPackageManager(); final List listCam = packageManager.queryIntentActivities(captureIntent, 0); for (ResolveInfo res : listCam) { final String packageName = res.activityInfo.packageName; final Intent i = new Intent(captureIntent); i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); i.setPackage(packageName); i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); cameraIntents.add(i); } Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); Intent chooserIntent = Intent.createChooser(i, "Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{})); this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); }
@Override protected void onDestroy() { super.onDestroy(); if (webview != null) webview.destroy(); }
/** * 使点击回退按钮不会直接退出整个应用程序而是返回上一个页面 * * */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK&&webview.canGoBack()){ webview.goBack();//返回上个页面 return true; }else {
finish();return false; }}