繁体   English   中英

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

[英]android- open default pdf viewer on click notification

我必须根据文件类型/ 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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM