从通知打开文件

Vas*_*las 4 android file android-intent

我从服务器下载文件。对于此操作,我将显示进度不确定的通知。当文件被下载时,我想通过点击通知来打开它。

我得到了扩展并尝试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;
}
Run Code Online (Sandbox Code Playgroud)

并在通知上执行此操作

Intent intent = ActionsHelper.openFile(context, file);
if (intent != null) {
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
      mBuilder.setContentIntent(pendingIntent);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,对于任何扩展,选择器上的消息是No app can perform this action。我错过了什么吗?

Vas*_*las 5

好的,我找到了。我替换

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)

它起作用了