以编程方式结束正在进行的通知 - Android

时间:2011-03-06 01:47:12

标签: android gps notifications android-service android-notifications

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

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

另外,我之前从未使用过GPS,但我是否应该在服务中进行此操作,这样如果我的应用程序被操作系统取出内存,用户就不会停止跟踪?或者这不会发生?

2 个答案:

答案 0 :(得分:9)

我对通知做的不多,但你可以试试这个:

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

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

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

答案 1 :(得分: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);