简体   繁体   中英

How do I let the user cancel a progress notification in Android?

I'm working on an Android app in which users can download content from a server. The user can click the download button, and then a post request is sent to the server and gets back a JSON String with the data. Some of the data in the JSON String are urls for photos, and so I give a notification that tracks the number of photos downloaded and uses that number to update a Progress Notification.

I have all of that working fine, and it's not causing me any troubles. The problem I'm running into now is more from the user perspective. If I see a downloading notification, I can swipe it away to dismiss it, but because I update my notification with a thread that loops while the photos are downloading, the thread will recreate the notification almost instantly after I've dismissed it.

I imagine this could be very annoying to a user, and was wondering how I can link the action of swiping to remove the notification with an interrupt to the Notification thread.

Are there any ideas on how to do this? Am I trying to solve a problem that isn't really a problem? Am I going about this the wrong way? Any feedback would be appreciated.

Here's my code for the notification thread:

    TripDownloadStatus prog = TripDownloadStatus.getStatus(notificationID);

    //Loops; reading the progress many times a second
    try{
        while(prog.getTotalTasks() == 0 || prog.getComplTasks() < prog.getTotalTasks()){

            Thread.sleep(200);  //Roughly 5 times a second
            prog = TripDownloadStatus.getStatus(notificationID);

            final int max = prog.getTotalTasks();
            final int cur = prog.getComplTasks();

            //Check if there are photos in progress before we go into an
            //infinite loop of waiting to do nothing.
            if(max == 0 && prog.isJSONParsed())
                break;

            this.theBuilder.setProgress(max, cur, false);
            manager.notify(this.notificationID, this.theBuilder.build());

        }
    } catch (InterruptedException ex){
        return;
    }

Making the notification dismissable with the deleteIntent is bad practice - if the user clears all notifications it will stop the download and that's probably not what the user intended. As a user, I don't expect to cancel downloads when clearing all the notifications via the clear-all button.

Your case is textbook case for an ongoing notification:

builder.setOngoing(true);

That way the user can't dismiss it - but now it becomes your responsibility to dismiss it when appropriate. When the download is done, in your case.

In order to let user cancel the operation you need to add a button to the notification using

builder.addAction(icon, title, intent);

When the user touches that button it will trigger onNewIntent() on your service and there you should put the cancellation code - stopping the download etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM