赞
踩
Tbs是
官方的接入文档有点坑 ,也在网上找了好几个博客 内容都是一样的
这里我总结下Tbs的使用 和 踩到的坑吧
- 去官网下载它的SDK :https://x5.tencent.com/tbs/sdk.html
- 直接使用Gradle拉取
implementation 'com.tencent.tbs.tbssdk:sdk:43903'
或
api 'com.tencent.tbs.tbssdk:sdk:43903'
- 复制jar包
- 右键添加jar包
- 在AndroidManifest.xml 中添加,
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.INSTALL_PACKAGES" tools:ignore="ProtectedPermissions" /> <!-- 硬件加速对X5视频播放非常重要,建议开启 --> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />
- AndroidManifest.xml 在6.0以后系统 是不行的,还需要配置 FileProvider,在application中加入如下:
<provider
android:name="com.tencent.smtt.utils.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/x5webview_file_paths" />
</provider>
<service
android:name="com.tencent.smtt.export.external.DexClassLoaderProviderService"
android:label="dexopt"
android:process=":dexopt" />
并且创建x5webview_file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="sdcard" path="."/>
</paths>
1.新键一个初始化类
/* * @DateTime 2020/6/30 9:37 * @Author zwb * @Des 初始化X5 * */ public class X5App { public void initX5app(Context context){ //设置非wifi条件下允许下载X5内核 QbSdk.setDownloadWithoutWifi(true); //搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。 QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() { @Override public void onViewInitFinished(boolean arg0) { //x5內核初始化完成的回调,true表x5内核加载成功,否则表加载失败,会自动切换到系统内核。 Log.d("app", " 内核加载 " + arg0); } @Override public void onCoreInitFinished() { } }; //x5内核初始化接口 QbSdk.initX5Environment(context, cb); } }
2.调用初始化类进行初始化,我是在启动app时就调用了
initX5Environment 的 onViewInitFinished 回调 一定要为True 如果是False则失败
之前我就是因为这里一直为false导致无法使用 这里有个坑 一定要确保为True!!!!!
@Override
public void onCreate() {
super.onCreate();
// 调用初始化X5
new X5App().initX5app(getApplicationContext());
}
1.创建X5WebView类
/* * @DateTime 2020/6/24 10:27 * @Author zwb * @Des 继承x5webview 做一些设置 * */ public class X5WebView extends WebView { private WebViewClient client = new WebViewClient() { /** * 防止加载网页时调起系统浏览器 */ public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }; @SuppressLint("SetJavaScriptEnabled") public X5WebView(Context arg0, AttributeSet arg1) { super(arg0, arg1); this.setWebViewClient(client); initWebViewSettings(); this.getView().setClickable(true); } private void initWebViewSettings() { WebSettings mWebSettings = this.getSettings(); mWebSettings.setJavaScriptEnabled(true); mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true); mWebSettings.setAllowFileAccess(true); mWebSettings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); mWebSettings.setSupportZoom(true); mWebSettings.setBuiltInZoomControls(true); mWebSettings.setUseWideViewPort(true); mWebSettings.setSupportMultipleWindows(true); mWebSettings.setAppCacheEnabled(true); mWebSettings.setDomStorageEnabled(true); mWebSettings.setGeolocationEnabled(true); mWebSettings.setAppCacheMaxSize(Long.MAX_VALUE); mWebSettings.setPluginState(WebSettings.PluginState.ON_DEMAND); mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); } public X5WebView(Context arg0) { super(arg0); setBackgroundColor(85621); } }
2.使用X5WebView
activity_x5_tbs_web_view_page.xml
<!--创建视图 ,这里布局 使用com.example.demo.x5.X5WebView 替代 WebView-->
<com.example.demo.x5.X5WebView
android:id="@+id/mX5WebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/title_row"
tools:ignore="MissingClass">
</com.example.demo.x5.X5WebView>
X5TbsWebViewPage.java
注意:这里面的导包 要导com.tencent.smtt.sdk下的包
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
/* * @DateTime 2020/6/29 16:51 * @Author zwb * @Des X5内核WebView页面 * */ public class X5TbsWebViewPage extends AppCompatActivity { private X5WebView mX5WebView ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_x5_tbs_web_view_page); mX5WebView = (X5WebView) findViewById(R.id.mX5WebView); mX5WebView.loadUrl("https://x5.tencent.com/"); } /*返回上个页面 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (mX5WebView != null && mX5WebView.canGoBack()) { mX5WebView.goBack(); return true; } else { return super.onKeyDown(keyCode, event); } } return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { //释放资源 if (mX5WebView != null){ mX5WebView.destroy(); } super.onDestroy(); } }
- 创建activity
activity_x5_tbs_file_service_page.xml
创建一个容器
<RelativeLayout
android:id="@+id/X5TbsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/title_row"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/colorPrimary"
/>
X5TbsFileServicePage.java
在Activity中 实现TbsReaderView.ReaderCallback
文件预览页面只能是生成的,
这里我把它生成到 容器里去 mRelativeLayout = findViewById(R.id.X5TbsView);
/* * @DateTime 2020/6/29 16:51 * @Author zwb * @Des X5内核文件预览页面*/ public class X5TbsFileServicePage extends AppCompatActivity implements TbsReaderView.ReaderCallback { private String tbsReaderTemp = Environment.getExternalStorageDirectory() + "/TbsReaderTemp"; TbsReaderView mTbsReaderView; RelativeLayout mRelativeLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_x5_tbs_file_service_page); // 绑定容器 mRelativeLayout = findViewById(R.id.X5TbsView); FileModel fileModel = new FileModel(); fileModel.setPath("/storage/emulated/0/demo/word.docx"); displayFile(fileModel); } public void displayFile(FileModel fileModel) { mTbsReaderView = new TbsReaderView(this,this); //增加下面一句解决没有TbsReaderTemp文件夹存在导致加载文件失败 String bsReaderTemp = tbsReaderTemp; File bsReaderTempFile =new File(bsReaderTemp); if (!bsReaderTempFile.exists()) { Log.d("print","文件不存在准备创建/TbsReaderTemp!!"); boolean mkdir = bsReaderTempFile.mkdir(); if(!mkdir){ Log.d("print","创建/TbsReaderTemp失败!!!!!"); } Toast.makeText(this,"文件不存在!",Toast.LENGTH_SHORT).show(); } mRelativeLayout.addView(mTbsReaderView,new RelativeLayout.LayoutParams(-1,-1)); Bundle bundle = new Bundle(); bundle.putString("filePath",fileModel.getPath()); bundle.putString("tempPath", tbsReaderTemp); boolean result = mTbsReaderView.preOpen(getFileType("word.docx"), false); Log.d("print","查看文档---"+result); if (result) { mTbsReaderView.openFile(bundle); }else{ } } private String getFileType(String paramString) { String str = ""; if (TextUtils.isEmpty(paramString)) { Log.d("print", "paramString---->null"); return str; } Log.d("print", "paramString:" + paramString); int i = paramString.lastIndexOf('.'); if (i <= -1) { Log.d("print", "i <= -1"); return str; } str = paramString.substring(i + 1); Log.d("print", "paramString.substring(i + 1)------>" + str); return str; } @Override public void onCallBackAction(Integer integer, Object o, Object o1) { Log.i("触摸监听:"," "); } /** * 返回上个页面 * @param keyCode * @param event * @return */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { super.onDestroy(); if (mTbsReaderView != null) { mTbsReaderView.onStop(); mTbsReaderView.destroyDrawingCache(); } } }
- 使用 我这里是跳转页面,FileModel 是一个实体类 自己定义自己要的参数即可
X5TbsFileServicePage x5TbsFileServicePage =new X5TbsFileServicePage();
FileModel fileModel = new FileModel();
fileModel.setPath("/storage/emulated/0/demo/pdf.pdf");
x5TbsFileServicePage.displayFile(fileModel);
Intent intent = new Intent();
intent.setClass(MainActivity.this, X5TbsFileServicePage.class);
startActivity(intent);
到此,结束
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。