以编程方式结束正在进行的通知-Android [英] Programmatically ending an ongoing notification - Android

查看:58
本文介绍了以编程方式结束正在进行的通知-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于GPS的应用程序,并且刚刚开始添加通知和进度条等UX功能,但是我坚持使用正在进行的通知.

I am developing a GPS-based app and have just started adding in my UX features such as notifications and progress bars but I'm stuck on using an ongoing notification.

因为它是GPS应用程序,所以当开始跟踪用户时,我会设置一个持续通知以显示正在跟踪他们,但是当他们在我的应用程序中点击停止跟踪"时如何停止此通知?我需要告诉NotifyManager吗?我基本上是在尝试获得音乐播放器所具有的功能,例如当用户按下播放键时会出现播放"通知,但是当他们暂停播放时,正在进行的播放"通知会被破坏.

As it is a GPS app, when user tracking is started, I set up an ongoing notification to show that they are being tracked but how do I stop this notification when they tap "stop tracking" in my app? Do I have to tell the NotifyManager something? I'm basically trying to get the functionality that music players have, as in the "playing" notification appears when the user presses play, but when they pause, that ongoing "playing" notification is destroyed.

此外,我以前从未使用过GPS,但我应该在Service中进行此操作,以便在OS占用我的应用程序内存时,不会停止跟踪用户吗?还是那不会发生?

Also, I've never worked with GPS before but should I be going about this in a Service so that the user won't stop being tracked if my app is taken out of memory by the OS? Or would that not happen?

推荐答案

我对通知的处理不多,但是您可以尝试以下操作:

I haven't done much with notifications but you might try this:

NotificationManager.cancel(id); // Where 'id' is the id of your notification

当然用您的实例名称替换NotificationManager.

Replacing NotificationManager with the name of your instance of it, of course.

docs: http://developer.android. com/reference/android/app/NotificationManager.html#cancel%28int%29

或者这个:

Notification.Builder.setAutoCancel(true);

http://developer.android. com/reference/android/app/Notification.Builder.html#setAutoCancel%28boolean%29

这篇关于以编程方式结束正在进行的通知-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆