0

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

用户选择并单击

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

  • DownloadManager.ACTION_DOWNLOAD_COMPLETE

  • DownloadManager.ACTION_NOTIFICATION_CLICKED

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

4

1 回答 1

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();
    }
}
于 2013-03-03T17:24:14.160 回答