Android DownloadManager处理完成下载时的通知单击事件

Android DownloadManager处理完成下载时的通知单击事件,android,download,notifications,mp3,Android,Download,Notifications,Mp3,我在尝试处理下载的.mp3文件的通知单击事件时遇到问题。我已经注册了以下内容: DownloadManager.ACTION\u NOTIFICATION\u CLICKED-我仅在单击当前正在下载的通知项目(仍在运行)时从广播接收器收到回调 DownloadManager.ACTION\u DOWNLOAD\u COMPLETE-下载完成后我会收到回调 问题是,当我点击一个已完成的下载时,我会得到一个可以处理该下载.mp3文件的应用程序列表(如VLC) 是否有办法限制通知项目的点击,使其仅

我在尝试处理下载的.mp3文件的通知单击事件时遇到问题。我已经注册了以下内容:

  • DownloadManager.ACTION\u NOTIFICATION\u CLICKED-我仅在单击当前正在下载的通知项目(仍在运行)时从广播接收器收到回调
  • DownloadManager.ACTION\u DOWNLOAD\u COMPLETE-下载完成后我会收到回调
问题是,当我点击一个已完成的下载时,我会得到一个可以处理该下载.mp3文件的应用程序列表(如VLC)

是否有办法限制通知项目的点击,使其仅由我的应用程序处理?顺便说一下,我的应用程序甚至没有显示在列表中

附言。 如果解决方案不需要创建自定义通知处理,那就太好了

编辑1: 以下是开始下载的代码:

private void downloadRequest(int position, String url, String title, String description, String filename) {

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDescription(description);
    request.setTitle(title);
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

    try {
        request.setDestinationUri(Uri.fromFile(new File(FileUtils.getAudioDirectory(getActivity()) + filename)));
    } catch (IOException e) {
        e.printStackTrace();
    }

    enqueue = mDownloadManager.enqueue(request);
根据matty357的要求,这是我的广播接收器,用于通知点击:

private BroadcastReceiver receiverNotificationClicked = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String extraId = DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS;
        long[] references = intent.getLongArrayExtra(extraId);
        for(long reference : references) {
            AppLog.d("DOWNLOAD_MANAGER", "reference (clicked): " + reference);
        }
        AppLog.d("DOWNLOAD_MANAGER", "Received click");
    }
};

您可以通过编程方式自己打开它

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/test.mp4");
intent.setDataAndType(Uri.fromFile(file), "video/*");
startActivity(intent); 

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File("/sdcard/test.mp3");
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent); 
使用intent.setPackage(“packageName”)


解决方法是:

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
因此,当下载完成时,您将不会收到通知。以及:

context.registerReceiver(onComplete,
            new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
因此,您将能够在下载完成时发出通知

BroadcastReceiver onComplete=new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        Log.d(TAG,"onComplete");
        // AND HERE SET YOUR ONW NOTIFICATION WHICH YOU WILL ABLE TO HANDLE
    }
};

不行,matty357。在没有我的应用程序的情况下,我仍然会得到一个“使用完整操作”的列表。仍然显示VLC等。请您发布正在单击接收器中使用的代码块。不确定这是否有帮助,但是,正如我上面所述,如果下载仍在进行中,则效果良好-我收到单击事件,因为我注册了DownloadManager.ACTION\u NOTIFICATION\u CLICKED.it,所以我们可以看到单击发生时您正在做什么好的,这只是日志记录。但是发布了我的接收器以及我如何开始下载。