简体   繁体   English

如何让用户取消Android中的进度通知?

[英]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. 我正在开发一个Android应用,用户可以在其中从服务器下载内容。 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. 用户可以单击下载按钮,然后将发布请求发送到服务器,并获取包含数据的JSON字符串。 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. JSON字符串中的某些数据是照片的网址,因此我给出了一个通知,该通知会跟踪下载的照片数量并使用该数量来更新进度通知。

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. 使用deleteIntent禁用通知是不明智的做法-如果用户清除了所有通知,它将停止下载,这可能不是用户想要的。 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. 当用户触摸该按钮时,它将触发您的服务上的onNewIntent(),您应该在其中放置取消代码-停止下载等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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