Cancel Google Drive upload possible?

后端 未结 4 666
长情又很酷
长情又很酷 2020-12-31 13:22

I have written an app using the official API to upload to Google drive, this works perfectly. However I can\'t find anyway to cancel an upload. I\'m running it in an ASync

4条回答
  •  时光说笑
    2020-12-31 14:13

    I've been looking for an answer for a long time as well. The following is the best I could come up with.

    Store the Future object anywhere you want (use Object or Void):

    Future future;
    
    
    

    Call this code (Java 8):

    try
    {
        ExecutorService executor = Executors.newSingleThreadExecutor();
    
        future = (Future) executor.submit(() ->
        {
           // start uploading process here
        });
    
        future.get();            // run the thread (blocks)
        executor.shutdown();
    }
    catch (CancellationException | ExecutionException | InterruptedException e)
    {}
    
    
    

    To cancel, call:

    future.cancel(true);
    

    This solution hasn't produced any nasty side effects so far, unlike stopping a thread (deprecated), and guarantees a cancel, unlike interrupting.

    提交回复
    热议问题