Android DownloadManager - 有没有办法在用户从UI删除下载时收到通知?

时间:2013-02-28 13:07:15

标签: android download-manager

当用户在系统的DownloadManager UI中取消下载时,我找不到通知方式:

user select and bin-click

我知道可以通过专门的意图操作为已下载“已完成”或“点击”设置BroadcastReceiver

  • DownloadManager.ACTION_DOWNLOAD_COMPLETE

  • DownloadManager.ACTION_NOTIFICATION_CLICKED

我需要知道何时取消正在运行的下载。

1 个答案:

答案 0 :(得分:0)

如上所述,我的解决方案(感谢来自SO的各种页面):

// DownloadManager job from the main activity

videoUri = Uri.parse(path.toURI() + composedFilename);

dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(link));
request.setDestinationUri(videoUri);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setTitle(vfilename);
enqueue = dm.enqueue(request);
Log.d(DEBUG_TAG, "_ID " + enqueue + " enqueued");

fileObserver = new Utils.delFileObserver(path.getAbsolutePath());
fileObserver.startWatching();

// delFileObserver class inside another Utility class

public static class delFileObserver extends FileObserver {
    static final String TAG="FileObserver: ";

    String rootPath;
    static final int mask = (FileObserver.CREATE | FileObserver.DELETE | FileObserver.DELETE_SELF); 

    public delFileObserver(String root){
        super(root, mask);

        if (! root.endsWith(File.separator)){
            root += File.separator;
        }
        rootPath = root;
    }

    public void onEvent(int event, String path) {

        if (event == FileObserver.DELETE || event == FileObserver.DELETE_SELF){
            Log.d(DEBUG_TAG, TAG + "file " + path + " DELETED");

            long id = settings.getLong(path, 0);
            Log.d(DEBUG_TAG, TAG + "id: " +  id);

            // actual job after a file deletion is detected
        }
    }

    public void close(){
        super.finalize();
    }
}