整合騰訊瀏覽器核心X5webview
阿新 • • 發佈:2019-02-14
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.ValueCallback<Uri[]> valueCallback, FileChooserParams fileChooserParams) { mUploadCallbackAboveL = valueCallback; takePhoto(); return true; } // Android < 3.0 呼叫這個方法 public void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; takePhoto(); } // 3.0 + 呼叫這個方法 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { mUploadMessage = uploadMsg; takePhoto(); } // Android > 4.1.1 呼叫這個方法 public void openFileChooser(ValueCallback<Uri> 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 List<Intent> cameraIntents = new ArrayList<Intent>(); final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = getPackageManager(); final List<ResolveInfo> 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; }}