WPS官方API:http://mo.wps.cn/wps-office-pro/index.html
根据官方API新建一个常量类
public class WpsModel {
/**
* 打开文件的模式。
*/
public static final String OPEN_MODE = "OpenMode";
/**
* 文件保存时是否发送广播
*/
public static final String SEND_SAVE_BROAD = "SendSaveBroad";
/**
* 文件关闭时是否发送广播
*/
public static final String SEND_CLOSE_BROAD = "SendCloseBroad";
/**
* 监听home键并发广播
*/
public static final String HOMEKEY_DOWN = "HomeKeyDown";
/**
* 监听back键并发广播
*/
public static final String BACKKEY_DOWN = "BackKeyDown";
/**
* 第三方的包名,关闭的广播会包含该项。
*/
public static final String THIRD_PACKAGE = "ThirdPackage";
/**
* 关闭文件时是否请空临时文件
*/
public static final String CLEAR_BUFFER = "ClearBuffer";
/**
* 关闭文件时是否删除使用记录
*/
public static final String CLEAR_TRACE = "ClearTrace";
/**
* 关闭文件时是否删除打开的文件
*/
public static final String CLEAR_FILE = "ClearFile";
/**
* 文件上次查看的进度
*/
public static final String VIEW_PROGRESS = "ViewProgress";
/**
* 是否自动跳转到上次查看的进度
*/
public static final String AUTO_JUMP = "AutoJump";
/**
* 文件保存路径
*/
public static final String SAVE_PATH = "SavePath";
/**
* 文件上次查看的视图的缩放
*/
public static final String VIEW_SCALE = "ViewScale";
/**
* 文件上次查看的视图的X坐标
*/
public static final String VIEW_SCALE_X = "ViewScrollX";
/**
* 文件上次查看的视图的Y坐标
*/
public static final String VIEW_SCALE_Y = "ViewScrollY";
/**
* 批注的作者
*/
public static final String USER_NAME = "UserName";
/**
* 以修订模式打开文档
*/
public static final String ENTER_REVISE_MODE = "EnterReviseMode";
/**
* Wps生成的缓存文件外部是否可见
*/
public static final String CACHE_FILE_INVISIBLE = "CacheFileInvisible";
public class OpenMode {
public static final String NORMAL = "Normal";// 正常模式
public static final String READ_ONLY = "ReadOnly";// 只读模式
public static final String READ_MODE = "ReadMode";// 打开直接进入阅读器模式 仅Word、TXT文档支持
// 仅Word、TXT文档支持
public static final String SAVE_ONLY = "SaveOnly";// 保存模式(打开文件,另存,关闭) 仅Word、TXT文档支持
// 仅Word、TXT文档支持
}
public class ClassName {
public static final String NORMAL = "cn.wps.moffice.documentmanager.PreStartActivity2";// 普通版
public static final String ENGLISH = "cn.wps.moffice.documentmanager.PreStartActivity2";// 英文版
public static final String ENTERPRISE = "cn.wps.moffice.documentmanager.PreStartActivity2";// 企业版
}
public class PackageName {
public static final String NORMAL = "cn.wps.moffice_eng";// 普通版
public static final String ENGLISH = "cn.wps.moffice_eng";// 英文版
public static final String ENTERPRISE = "cn.wps.moffice_ent";// 企业版
}
public class Reciver {
public static final String ACTION_BACK = "com.kingsoft.writer.back.key.down";// 返回键广播
public static final String ACTION_HOME = "com.kingsoft.writer.home.key.down";// Home键广播
public static final String ACTION_SAVE = "cn.wps.moffice.file.save";// 保存广播
public static final String ACTION_CLOSE = "cn.wps.moffice.file.close";// 关闭文件广播
}
public class ReciverExtra {
//关闭文件广播返回的信息
public static final String CLOSEFILE = "CloseFile";//关闭文件的路径
public static final String THIRDPACKAGE = "ThirdPackage";//传入的第三方的包名
public static final String VIEWPROGRESS = "ViewProgress";//文件查看的进度
public static final String VIEWSCALE = "ViewScale";//文件上次查看的视图的缩放
public static final String VIEWSCROLLX = "ViewScrollX";//文件上次查看的视图的X坐标
public static final String VIEWSCROLLY = "ViewScrollY";//文件上次查看的视图的Y坐标
//保存文件广播返回的信息
public static final String OPENFILE = "OpenFile";//文件最初的路径
public static final String SAVEPATH = "SavePath";//文件这次保存的路径
}
}
通过startActivity方式使用WPS打开文件
public static void openFile(Context context,File file){
Bundle bundle = new Bundle();
bundle.putString(WpsModel.OPEN_MODE, WpsModel.OpenMode.NORMAL); // 正常模式
bundle.putBoolean(WpsModel.SEND_CLOSE_BROAD, true); // 文件关闭时是否发送广播
bundle.putBoolean(WpsModel.SEND_SAVE_BROAD, true); // 文件保存时是否发送广播
bundle.putBoolean(WpsModel.HOMEKEY_DOWN, true); // 单机home键是否发送广播
bundle.putBoolean(WpsModel.BACKKEY_DOWN, true); // 单机back键是否发送广播
bundle.putString(WpsModel.THIRD_PACKAGE, WpsModel.PackageName.NORMAL); // 第三方应用的包名,用于对改应用合法性的验证
bundle.putBoolean(WpsModel.CLEAR_FILE, false); //关闭后是否删除打开文件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName(WpsModel.PackageName.NORMAL, WpsModel.ClassName.NORMAL);
intent.putExtras(bundle);
uriX(context, intent, file);
}
public static void uriX(Context context, Intent intent, File file) {
if (Build.VERSION.SDK_INT > 23) {//android 7.0以上时,URI不能直接暴露
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//另如果使用的androidx:androidx.core.content.FileProvider替换android.support.v4.content.FileProvider
Uri uriForFile = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file);
intent.setDataAndType(uriForFile, "application/vnd.android.package-archive");
} else {
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
}
try {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
ToastUtil.showToast(context, R.string.please_check_wps);
e.printStackTrace();
}
}
监听WPS操作的广播
public class WpsBroadCastReciver extends BroadcastReceiver {
private final String TAG = "WpsBroadCastReciver-->";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
LogUtil.e(TAG, "onReceive : wps广播 --> " + action);
if (action == null) {
return;
}
switch (action) {
case WpsModel.Reciver.ACTION_BACK://返回键
LogUtil.e(TAG, "onReceive : 返回键 --> ");
break;
case WpsModel.Reciver.ACTION_CLOSE://关闭文件时的广播
String closeFile = intent.getStringExtra(WpsModel.ReciverExtra.CLOSEFILE);
String thirdPackage1 = intent.getStringExtra(WpsModel.ReciverExtra.THIRDPACKAGE);
LogUtil.e(TAG, "onReceive : 关闭文件收到广播 --> closeFile:" + closeFile + ", \n thirdPackage:" + thirdPackage1);
break;
case WpsModel.Reciver.ACTION_HOME://home键广播
LogUtil.e(TAG, "onReceive : home键广播 --> ");
break;
case WpsModel.Reciver.ACTION_SAVE://保存文件时的广播
String openFile = intent.getStringExtra(WpsModel.ReciverExtra.OPENFILE);//文件最初的路径
String thirdPackage = intent.getStringExtra(WpsModel.ReciverExtra.THIRDPACKAGE);//传入的第三方的包名。
String savePath = intent.getStringExtra(WpsModel.ReciverExtra.SAVEPATH);//文件这次保存的路径
LogUtil.e(TAG, "onReceive : 保存键广播 --> \n文件最初的路径: "
+ openFile + "\n传入的第三方的包名:" + thirdPackage + "\n文件这次保存的路径:" + savePath);
break;
}
}
}
FileProvider官方文档:https://developer.android.google.cn/reference/android/support/v4/content/FileProvider.html
步骤:1.在AndroidManifest.xml中添加如下代码
2.在res目录下新建一个xml文件夹,并且新建一个provider_paths的xml文件
3.使用到uri的代码
Uri photoURI = Uri.fromFile(createImageFile());
变成:
//另:androidx.core.content.FileProvider替换android.support.v4.content.FileProvider
Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());
只需要在项目的Application中初始化使用
/**
* 使用VmPolicy方式检测FileUriExposure异常,解决7.0路径问题
*/
private void initVmPolicy() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}
}