赞
踩
集成tbs
第一步:下载jar包点击打开链接 本文是compile files('libs/tbs_sdk_thirdapp_v3.6.0.1234_43608_sharewithdownload_withoutGame_obfs_20180510_111111.jar')
把下载的jar包放在libs下,在build.gradle添加依赖,复制以上代码即可,
第二步:把下载的.so文件直接放在jniLibs下的armeabi文件夹下即可,或者放在libs下然后添加依赖
第三步:在application中初始化x5内核(最好是手机内安装一款腾讯的产品,QQ,微信,QQ浏览器等,因为会共享他们的x5内核),
代码如下public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
@Override
public void onCoreInitFinished() {
}
@Override
public void onViewInitFinished(boolean b) {
Log.e("TAG", "加载x5内核是否成功==" + b);
}
};
QbSdk.initX5Environment(getApplicationContext(), cb);
}
}
第四步:如何调用展现文档呢?直接上代码public class TbsActivity extends AppCompatActivity implements TbsReaderView.ReaderCallback {
private FrameLayout root;
private TbsReaderView tbsReaderView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tbs_activity);
GrantExternalRW.isGrantExternalRW(this);//6.0以后动态权限的判断
fullScreen(this);//全屏
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
root = findViewById(R.id.root);
tbsReaderView = new TbsReaderView(this, this);
root.addView(tbsReaderView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
@Override
protected void onStart() {
super.onStart();
Intent intent = getIntent();
String fileName = intent.getStringExtra("fileName");
displayFile(fileName);//展现文档
}
@Override
public void onCallBackAction(Integer integer, Object o, Object o1) {
}
/**
* 通过设置全屏,设置状态栏透明
*
* @param activity
*/
private void fullScreen(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色
Window window = activity.getWindow();
View decorView = window.getDecorView();
//两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
decorView.setSystemUiVisibility(option);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
//导航栏颜色也可以正常设置
// window.setNavigationBarColor(Color.TRANSPARENT);
} else {
Window window = activity.getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
attributes.flags |= flagTranslucentStatus;//设置全屏
attributes.flags |= flagTranslucentNavigation;//设置是否显示标题栏
window.setAttributes(attributes);
}
}
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android6.0以后可以对状态栏文字颜色和图标进行修改
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}*/
}
private void displayFile(String fileName) {
Bundle bundle = new Bundle();
bundle.putString(TbsReaderView.KEY_FILE_PATH, 文件的全路径);
bundle.putString(TbsReaderView.KEY_TEMP_PATH, 缓存存放的路径);
boolean b = tbsReaderView.preOpen(parseFormat(fileName), false);//第一个参数代表问的的格式
if (b) {
tbsReaderView.openFile(bundle);//打开文档
}
}
private String parseFormat(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
@Override
protected void onDestroy() {
super.onDestroy();
tbsReaderView.onStop();//重要的是释放
}
具体的解释在代码中已经解释,最重要的是要在onDestroy()方法中释放资源,就像关闭数据库连接一样
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。