【问题标题】:Android DownloadManager - Is there a way to be notified when a user deletes a download from the UI?Android DownloadManager - 有没有办法在用户从 UI 中删除下载时收到通知?
【发布时间】:2015-07-04 02:36:12
【问题描述】:

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

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

  • DownloadManager.ACTION_DOWNLOAD_COMPLETE

  • DownloadManager.ACTION_NOTIFICATION_CLICKED

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

【问题讨论】:

  • 看来通过 DownloadManager 本身是不可能的,所以我想我会使用FileObserver 并在这里发布结果作为任何人将来需要的答案。

标签: android download-manager


【解决方案1】:

如上所述,我的解决方案(感谢 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();
    }
}

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2013-02-05
    • 2017-09-18
    相关资源
    最近更新 更多