
腾讯TBS是X5内核的升级版,可以当作webvIEw 来打开 网页,可以以用来打开docx doc pdf 等文件,这里主要使用的是文件功能。
依赖接入 API 'com.tencent.tbs.tbssdk:sdk:43939' 这是笔者2021/2/25编辑时最新版本,最新可在官网查询。
如果依赖文件下载有问题可手动下载jia包,本地依赖包可放在 app/libs 下,文件夹可自行创建,同时在 app/src/main/jnilibs/armeabi 下存放.so 文件。
再module 的gradle文件defaultConfig下添加
ndk { abiFilters "armeabi", "x86", "mips", "armeabi-v7a" }这是为了避免64位手机不兼容的情况,强制打包。部分博客仅添加了 “armeabi” 一项。
TBS 的使用1 在继承的application中初始化
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() { @OverrIDe public voID onVIEwInitFinished(boolean arg0) { //x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。 Log.e(TAG, "加载内核是否成功:" + arg0); } @OverrIDe public voID onCoreInitFinished() { Log.e(TAG, "加载内核是否成功:"); } }; //x5内核初始化接口 QbSdk.initX5Environment(getApplicationContext(), cb);这里解释下,initX5Environment初始化方法中回调是可以写做 null的,但为了验证是否成功加载内核,还是有必要重写一下方法。同时软件需要获取以下权限:
<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" />注意动态权限读写内存和读取手机状态的获取。
2 TBS的两种使用
第一种是d出式框使用
filepath = "/storage/emulated/0/aaa.docx"; HashMap<String, String> params = new HashMap<String, String>(); JsONObject JsonObject=new JsONObject(); try { JsonObject.put("pkgname", DocxActivity.this.getApplication().getPackagename()); } catch (JsONException e) { e.printstacktrace(); } params.put("style", "1"); params.put("local", "true"); //进入文件查看器 params.put("memuData", JsonObject.toString()); QbSdk.openfileReader(this,filepath, params,this);结果如图所示
文件路径我使用的是根目录,目前TBS不支持网络预览文件,因此需要下载后才能打开。hashmap的参数说明如下,style是界面风格,详细可查看官方文档,local是是否进入本地文件管理器,memuData是菜单选项。由Json写入。
第二种是使用自己activity 创建自定义view,这种形式更加灵活,可定制性更强。
public class MyTbsReadVIEw extends FrameLayout implements TbsReaderVIEw.ReaderCallback { private static final String TAG = "MyTbsReadVIEw"; private TbsReaderVIEw tbsReaderVIEw; private int saveTime = -1; private Context context; private getfilepathListener getfilepathListener; public MyTbsReadVIEw(@NonNull Context context) { this(context, null, 0); } public MyTbsReadVIEw(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public MyTbsReadVIEw(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); tbsReaderVIEw = new TbsReaderVIEw(context, this); this.addVIEw(tbsReaderVIEw, new linearLayout.LayoutParams(-1, -1)); this.context = context; } public voID show(){ if (getfilepathListener != null) { getfilepathListener.getfilePath(this); } } private TbsReaderVIEw getTbsVIEw(Context context) { return new TbsReaderVIEw(context, this); } public voID display(file file){ if (file != null&& !TextUtils.isEmpty(file.toString())) { String tempfilefolder="/storage/emulated/0/TbsReaderTemp"; file tempfile=new file(tempfilefolder); if (!tempfile.exists()) { boolean flag= tempfile.mkdir(); if (flag) { Log.e(TAG, "display: success" ); } else { Log.e(TAG, "display: faile" ); } } Bundle bundle =new Bundle(); bundle.putString("filePath", file.toString()); bundle.putString("tempPath", Environment.getExternalStorageDirectory()+"/"+"TbsReaderTemp"); if (tbsReaderVIEw == null) { this.tbsReaderVIEw=getTbsVIEw(context); } if (this.tbsReaderVIEw.preOpen(getfileType(file.toString()),false)) { this.tbsReaderVIEw.openfile(bundle); } }else { Log.e(TAG, "display: file path doesn't exists" ); } } private String getfileType(String toString) { String str=""; if (TextUtils.isEmpty(toString)) { return str; } int i=toString.lastIndexOf("."); if (i <= -1) { return str; } str=toString.substring(i+1); return str; } public voID setGetfilepathListener(getfilepathListener Listener) { this.getfilepathListener = Listener; } @OverrIDe public voID onCallBackAction(Integer integer, Object o, Object o1) { Log.e(TAG, "onCallBackAction: "+integer ); } public voID onStop(){ if (tbsReaderVIEw != null) { tbsReaderVIEw.onStop(); } } public interface getfilepathListener { voID getfilePath(MyTbsReadVIEw myTbsReadVIEw); }}这里是自定义view代码 。继承framelayout。使用回调填充TbsReaderVIEw,关键代码为以下:
Bundle bundle =new Bundle(); bundle.putString("filePath", file.toString()); bundle.putString("tempPath", Environment.getExternalStorageDirectory()+"/"+"TbsReaderTemp"); if (tbsReaderVIEw == null) { this.tbsReaderVIEw=getTbsVIEw(context); } if (this.tbsReaderVIEw.preOpen(getfileType(file.toString()),false)) { this.tbsReaderVIEw.openfile(bundle); }tbsReaderVIEw 传入bundle值,分别为文件路径和临时文件路径,需要注意的是TbsReaderTemp文件夹需要事先创建,但只要手机有腾讯系软件如QQ,微信,此文件夹会事先存在。
在activity中使用代码如下:
filepath = "/storage/emulated/0/aaa.docx"; file myfile=new file(filepath); Log.e(TAG, "initVIEw: "+myfile.length() ); myTbsReadVIEw.setGetfilepathListener(new MyTbsReadVIEw.getfilepathListener() { @OverrIDe public voID getfilePath(MyTbsReadVIEw myTbsReadVIEw) { myTbsReadVIEw.display(myfile); } }); myTbsReadVIEw.show();添加文件导入监听,触发时使用show函数,需要注意的是需要在activity销毁时调用组件的onStop函数,否则再次打开会失败。结果如下:
笔者在使用时是创建demo的方式,还是遇到了不少问题,这里需要说明下:
1需要非安全http传输设置 即 manifest中networkSecurityConfig 和usesCleartextTraffic的两个属性。 2在Android10以上非专有文件读写需要设置,常用的方法是减低限制 ,在nanifest文件中requestLegacyExternalStorage`属性设置为true
3 manifest中provIDer
<!-- 非AndroIDX使用 androID:name="androID.support.v4.content.fileProvIDer"--> <provIDer androID:name="androIDx.core.content.fileProvIDer" androID:authoritIEs="${applicationID}" androID:exported="false" androID:grantUriPermissions="true"> <Meta-data androID:name="androID.support.file_PROVIDER_PATHS" androID:resource="@xml/provIDer_file_paths"/> </provIDer>provIDer_file_path文件如下
<?xml version="1.0" enCoding="utf-8"?><resources> <paths> <external-path path="" name="sdcard"/> </paths></resources>4 X5内核加载失败,具体表现为 “ not supported by:doc” 这个问题困扰笔者许久,最后的解决方式是在继承的application文件中,即初始化X5中添加
QbSdk.setTbsListener(new TbsListener() { @OverrIDe public voID onDownloadFinish(int i) { } @OverrIDe public voID onInstallFinish(int i) { Log.e(TAG, "onInstallFinish: 内核下载安装成功" ); } @OverrIDe public voID onDownloadProgress(int i) { } }); boolean needDownload = TbsDownloader.needDownload(this, TbsDownloader.DOWNLOAD_OVERSEA_TBS); Log.e(TAG, "onCreate: "+needDownload ); if (needDownload) { TbsDownloader.startDownload(this); } TUIKit.init(this, GenerateTestUserSig.SDKAPPID, new ConfigHelper().getConfigs());在加载内核失败后重新下载,最后log提示安装成功。
总结以上是内存溢出为你收集整理的Android 开发学习进程0.28 腾讯TBS接入和相关问题全部内容,希望文章能够帮你解决Android 开发学习进程0.28 腾讯TBS接入和相关问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)