需求:在app内下载完office文件后,调用wps直接打开文件,代码如下
Intent intent = context.getPackageManager().getLaunchIntentForPackage("cn.wps.moffice_eng");
if (intent == null) {
ToastUtil.show(context, "请先安装WPS Office 软件");
} else {
Bundle bundle = new Bundle();
bundle.putString("OpenMode", "ReadOnly");
bundle.putBoolean("BackKeyDown", true);
bundle.putString("ThirdPackage", context.getPackageName());
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file);
} else {
uri = Uri.fromFile(file);
}
intent.setData(uri);
intent.putExtras(bundle);
context.startActivity(intent);
}
而Android 11及更高版本更改了应用查询用户已在设备上安装的其他应用以及与之交互的方式,若需要查询第三方应用信息,需要在应用的清单文件中添加 元素。在 元素中,提供程序授权指定软件包。如下
<queries>
<!--wps-->
<package android:name="cn.wps.moffice_eng" />
<!--相机-->
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>