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

时间:2015-08-07 14:24:52

标签: android pdf notifications

我必须在默认应用程序中打开下载文件的onclick通知,具体取决于文件类型/ MIME。 例如- 如果下载的文件是PDF,则应在PDF查看器中打开,如果下载的文件是图像,则应在图库应用程序等中打开。

我有以下代码可以正常使用,但此代码中使用的某些功能已弃用,如通知 setLatestEventInfo 等。

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());

请有人告诉我如何使用上面给出的代码执行此操作?我的代码出了什么问题?

1 个答案:

答案 0 :(得分:7)

我在我的新代码中遗漏了一些行,我在旧代码中执行了这些行 -

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());