打开第三方应用选择并过滤掉不想被打开的应用

由于最近做项目时,客户需要打开各种文档,比如ppt,ppts,doc,docx,pdf,txt等文件,当用户没有安装打开这些文件的第三方程序时,会自动调用QQ,让用户感觉不好,这里我们做了一下过滤。
File file = new File(“文件名”);
// 文件file的MIME类型 打开
Intent intent = new Intent(Intent.ACTION_VIEW);
String fileName= file.getAbsolutePath();
String type=”“;
//这里例举了各种文档格式的所需的Type值
if (fileName.endsWith(“txt”)) {
type=”text/plain”;
} else if (fileName.endsWith(“doc”)) {
type=”application/msword”;
} else if (fileName.endsWith(“docx”)) {
type=”application/vnd.openxmlformats-officedocument.wordprocessingml.document”;
} else if (fileName.endsWith(“pptx”)) {
type=”application/vnd.openxmlformats-officedocument.presentationml.presentation”;
} else if (fileName.endsWith(“ppt”)) {
type=”application/vnd.ms-powerpoint”;
} else if (fileName.endsWith(“xls”)) {
type=”application/vnd.ms-excel”;
} else if (fileName.endsWith(“xlsx”)) {
type=”application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”;
} else if (fileName.endsWith(“pdf”)) {
type= “application/pdf”;
}
intent.setDataAndType(Uri.fromFile(file), type);

List resInfo = getPackageManager().queryIntentActivities(intent, 0);
if (!resInfo.isEmpty()) {
List targetedShareIntents = new ArrayList();
for (ResolveInfo info : resInfo) {
Intent targeted = new Intent(Intent.ACTION_VIEW);
targeted.setDataAndType(Uri.fromFile(file), type);
ActivityInfo activityInfo = info.activityInfo;
//在这里根据包名过滤掉不想选择的应用,比如QQ
if (activityInfo.packageName.contains(“com.tencent.mobileqq”)) {
continue;
}
targeted.setPackage(activityInfo.packageName);
targetedShareIntents.add(targeted);
}
if (targetedShareIntents.size()!=0){
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),”请你选择以下应用打开”);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}else {
Toast.makeText(this, “没有可选程序”, Toast.LENGTH_SHORT).show();
}
“`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值