我从服务器下载文件。对于此操作,我会显示一个包含不确定进度的通知。当我下载文件时,我想点击通知打开它。
我获得了扩展程序并尝试使用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);
}
我的问题是,对于任何扩展程序,选择器上的消息是没有应用程序可以执行此操作。我错过了什么吗?
答案 0 :(得分:2)
好的,我找到了。我替换
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
带
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
并且有效
答案 1 :(得分:0)
您正在设置Intent数据,并在创建选择器Intent后输入。切换到:
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, type);
Intent chooser = Intent.createChooser(intent, context.getResources().getString(R.string.open_file_with));