我从服务器下载一个文件。对于此操作,我显示了进度不确定的通知。下载文件后,我想通过单击通知来打开它。
我获得了扩展名并尝试使用以下命令打开它intent
像这样
public static Intent openFile(Context context, File file) {
String fileName = file.getName();
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
String type = "application/" + extension;
if (getAttachmentType(fileName) == AttachmentType.IMAGE) {
type = "image/*";
} else if (getAttachmentType(fileName) == AttachmentType.AUDIO) {
type = "audio/*";
} else if (getAttachmentType(fileName) == AttachmentType.VIDEO) {
type = "video/*";
} else if (getAttachmentType(fileName) == AttachmentType.WORD) {
type = "application/msword";
} else if (getAttachmentType(fileName) == AttachmentType.EXCEL) {
type = "application/vnd.ms-excel";
} else if (getAttachmentType(fileName) == AttachmentType.POWERPOINT) {
type = "application/vnd.ms-powerpoint";
} else if (getAttachmentType(fileName) == AttachmentType.TXT) {
type = "text/*";
}
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooser = Intent.createChooser(intent, context.getResources().getString(R.string.open_file_with));
intent.setDataAndType(path, type);
return chooser;
}
并在通知上执行此操作
Intent intent = ActionsHelper.openFile(context, file);
if (intent != null) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent);
}
我的问题是,对于任何扩展,选择器上的消息都是没有应用程序可以执行此操作。我错过了什么吗?
好吧,我找到了。我替换
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
with
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
它起作用了
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)