android- 在点击通知时打开默认的 pdf 查看器

android- open default pdf viewer on click notification

我必须根据文件 type/MIME 在默认应用程序中打开该文件的下载文件 onclick 通知。 例如- 如果下载的文件是 PDF,它应该在 PDF 查看器中打开,如果下载的文件是图像,它应该在图库应用程序等中打开。

我有下面的代码可以正常工作,但是这段代码中使用的一些函数已被弃用,如 NotificationsetLatestEventInfo 等。

public void notification(String file, String title, String message){
        NotificationManager nm= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification n = new Notification(R.drawable.icon,"Download Complete",System.currentTimeMillis());
        Context c= getApplicationContext();
        String text=file+message;

        Intent intent= new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        File f= new File(Environment.getExternalStorageDirectory()+File.separator+WelcomeActivity.app_files_dir_name+File.separator+file );
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        String ext=f.getName().substring(f.getName().indexOf(".")+1);
        String type = mime.getMimeTypeFromExtension(ext);
        intent.setDataAndType(Uri.fromFile(f), type);
        try {
            PendingIntent i = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
            n.defaults |=Notification.DEFAULT_SOUND;
            n.flags|=Notification.FLAG_AUTO_CANCEL;
            n.setLatestEventInfo(c, title, text, i);
            nm.notify(1, n);
        } catch (android.content.ActivityNotFoundException e) {
            Toast.makeText(MainActivity.this, "File is not supported.", Toast.LENGTH_LONG).show();
        }

    }

下面给出了我正在处理的新代码,但不工作-

File file = new File(filePath);
Intent openFile = new Intent(Intent.ACTION_VIEW,Uri.fromFile(file));
openFile.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent p = PendingIntent.getActivity(getContext(), 0, openFile, 0);
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(Welcome.this);
mBuilder.setContentTitle("Download").setContentText("Download in progress").setSmallIcon(R.drawable.icon).setContentIntent(p);
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(id, mBuilder.build());

谁能告诉我如何使用上面给出的代码做到这一点?我的代码有什么问题?

我在新代码中遗漏了一些我在旧代码中所做的行-

MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=f.getName().substring(f.getName().indexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(f), type);

所以最后的工作代码是-

String filePath = APP_DOWNLOAD_DIR + filename;
File file = new File(filePath);
Log.d(AppConfig.APP_TAG, "File to download = " + String.valueOf(file));
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=file.getName().substring(file.getName().indexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
Intent openFile = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
openFile.setDataAndType(Uri.fromFile(file), type);
openFile.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent p = PendingIntent.getActivity(getContext(), 0, openFile, 0);
mNotifyManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(Welcome.this);
mBuilder.setContentTitle("Download")
                            .setContentText("Download in progress")
                            .setSmallIcon(R.drawable.icon)
                            .setContentIntent(p);
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(id, mBuilder.build());