5

我正在开发一个基于 GPS 的应用程序,并且刚刚开始添加我的 UX 功能,例如通知和进度条,但我坚持使用持续通知。

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

另外,我以前从未使用过 GPS,但我是否应该在服务中进行此操作,以便在我的应用程序被操作系统占用内存时不会停止跟踪用户?或者那不会发生?

4

2 回答 2

9

我没有做太多通知,但你可以试试这个:

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

当然,替换NotificationManager为您的实例名称。

文档: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

于 2011-03-06T03:14:30.367 回答
3

开始

int id = 01;
NotificationCompat.Builder mBuilder =
      new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getResources().getString(R.string.service_header))
        .setContentText("Connect to: http://" + httpd.getip() + ":8080")
        .setContentIntent(pendingIntent)
        .setOngoing(true);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(01, mBuilder.build());

取消

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(01);
于 2015-01-02T13:19:41.353 回答