App、BrowserActivity、X5WebView
Related implementations in other categories inthe general access reference project on the TBS official website .
Step 1: Download the jar package and add it to the project
Step 2: Android studio modify related configuration
- Open the build.gradle added in the corresponding module
android{ defaultConfig{ ndk{abiFilters "armeabi"} } }
- If an error is reported after compiling after configuration, you need to add it to gradle.properties
Android.useDeprecatedNdk=true
src/main/
Create under a directory ;jniLibs
create aarmeabi
directory in it and addliblbs.so
files
Step 3: Permission declaration in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- Hardware acceleration is very important for X5 video playback, it is recommended to enable it --> <uses-permission android:name="android.permission.GET_TASKS"/>
Step 4: Initialize the x5 kernel interface in the Application
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() { @Override public void onViewInitFinished(boolean b) { //The callback for the completion of the x5 kernel initialization. If true, it means that the x5 kernel is loaded successfully. Otherwise, it means that the x5 kernel fails to load, and it will automatically switch to the system kernel. Log.d("app", " onViewInitFinished is " + b); } @Override public void onCoreInitFinished() { // TODO Auto-generated method stub } }; //x5 kernel initialization interface QbSdk.initX5Environment(getApplicationContext(), cb);
Step 5: Create X5WebView to inherit WebView in SDK
declare WebViewClient
private WebViewClient client = new WebViewClient() { /** * Prevent the system browser from being called up when loading a webpage */ public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } };
Set WebViewClient
@SuppressLint("SetJavaScriptEnabled") public X5WebView(Context arg0, AttributeSet arg1) { super (arg0, arg1); this.setWebViewClient(client); initWebViewSettings(); this.getView().setClickable(true); }
Initialize WebViewSettings
private void initWebViewSettings() { WebSettings webSetting = this.getSettings (); webSetting.setJavaScriptEnabled(true); webSetting.setJavaScriptCanOpenWindowsAutomatically(true); webSetting.setAllowFileAccess(true); webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); webSetting.setSupportZoom(true); webSetting.setBuiltInZoomControls(true); webSetting.setUseWideViewPort(true); webSetting.setSupportMultipleWindows(true); webSetting.setAppCacheEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE); }
drawChild in some WebView
@Override protected boolean drawChild(Canvas canvas, View child, long drawingTime) { boolean ret = super.drawChild(canvas, child, drawingTime); canvas.save(); Paint paint = new Paint(); paint.setColor(0x7fff0000); paint.setTextSize(24.f); paint.setAntiAlias(true); if (getX5WebViewExtension() != null) { canvas.drawText(this.getContext().getPackageName() + "-pid:" + android.os.Process.myPid(), 10, 50, paint); canvas.drawText( "X5 Core:" + QbSdk.getTbsVersion(this.getContext()), 10, 100, paint); } else { canvas.drawText(this.getContext().getPackageName() + "-pid:" + android.os.Process.myPid(), 10, 50, paint); canvas.drawText("Sys Core", 10, 100, paint); } canvas.drawText(Build.MANUFACTURER, 10, 150, paint); canvas.drawText(Build.MODEL, 10, 200, paint); canvas.restore(); return ret; }
Step 6: Adjust the use of cookies
The invocation of the related interfaces of com.tencent.smtt.sdk.CookieManager and com.tencent.smtt.sdk.CookieSyncManager needs to be placed after the X5 WebView is created (that is, the X5 kernel is loaded) after accessing the SDK; otherwise , cookie-related operations can only affect the system kernel.
Available in the project BrowserActivity
category initWebViewSetting()
/** * init WebView */ private void initWebViewSetting() { WebSettings webSetting = mWebView.getSettings(); webSetting.setAllowFileAccess(true); webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); webSetting.setSupportZoom(true); webSetting.setBuiltInZoomControls(true); webSetting.setUseWideViewPort(true); webSetting.setSupportMultipleWindows(false); webSetting.setAppCacheEnabled(true); webSetting.setDomStorageEnabled(true); webSetting.setJavaScriptEnabled(true); webSetting.setGeolocationEnabled(true); webSetting.setAppCacheMaxSize(Long.MAX_VALUE); webSetting.setAppCachePath(this.getDir("appcache", 0).getPath()); webSetting.setDatabasePath(this.getDir("databases", 0).getPath()); webSetting.setGeolocationDatabasePath(this.getDir("geolocation", 0) .getPath()); webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND); long time = System.currentTimeMillis(); if (mIntentUrl == null) { mWebView.loadUrl(mHomeUrl); } else { mWebView.loadUrl(mIntentUrl.toString()); } // print the time of webView load TbsLog.d("time-cost", "cost time: " + (System.currentTimeMillis() - time)); // manage Cookie CookieSyncManager.createInstance(this); CookieSyncManager.getInstance().sync(); }
Step 7: Compatible with video playback
1) To enjoy the full playback experience of the page video, the following statement is required:
The Activity of the page needs to declare android:configChanges=”orientation|screenSize|keyboardHidden”
2) In order to avoid the problem of splash screen, the following statement is required:
- Activity needs to be set when onCreate: getWindow().setFormat(PixelFormat.TRANSLUCENT); (this has no effect on the host, it is recommended to declare)
- The following interfaces are prohibited (direct or reflected) calls to prevent the video screen from being displayed:
webview.setLayerType();webview.setDrawingCacheEnabled(true);
Step 8: Avoid the problem of blocking the input cursor after the input method interface pops up
Method 1 : Set in AndroidManifest.xml
android:windowSoftInputMode="stateHidden|adjustResize"
Method 2 : Dynamically set in code
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Supplement: Tbs video player access instructions
- Step 1: AndroidManifest requires the following registration:
<!--Video--> <activity android:name="com.tencent.smtt.sdk.VideoActivity" android:alwaysRetainTaskState="true" android:configChanges="orientation|screenSize|keyboardHidden" android:exported="false" android:launchMode="singleTask"> <intent-filter> <action android:name="com.tencent.smtt.tbs.video.PLAY"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
- 第二步:通过TbsVideo调用播放视频,如下:
-
public static boolean canUseTbsPlayer(Context context) //判断当前Tbs播放器是否已经可以使用。 public static void openVideo(Context context, String videoUrl) //直接调用播放接口,传入视频流的url public static void openVideo(Context context, String videoUrl, Bundle extraData) //extraData对象是根据定制需要传入约定的信息,没有需要可以传如null //判断当前Tbs播放器是否已经可以使用。 if (TbsVideo.canUseTbsPlayer(MainActivity.this)) { //Directly call the playback interface and pass in the url of the video stream TbsVideo.openVideo(MainActivity.this, "http://192.168.3.108:8080/alert_icon.mp4"); }
source code attached