Android DownloadManager - Is there a way to be notified when a user deletes a download from the UI?

StackOverflow https://stackoverflow.com/questions/15136348

  •  16-03-2022
  •  | 
  •  

質問

I cannot find a way to be notified when a user cancels a download in the system's DownloadManager UI:

user select and bin-click

I know that it is possible to set a BroadcastReceiver for downloads "completed" or "clicked", via the dedicated intent actions:

  • DownloadManager.ACTION_DOWNLOAD_COMPLETE

and

  • DownloadManager.ACTION_NOTIFICATION_CLICKED

I need to know when a running download is cancelled, instead.

役に立ちましたか?

解決

As commented above, my solution (thanks to various pages from 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();
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top