Programmatically stop current notification - Android

I am developing a GPS-based application and have just started adding features to my UX, such as notifications and progress bars, but I'm stuck with a constant notification.

Since this is a GPS application, when I started tracking users, I set up a current notification to show that they were being tracked, but how do I stop this notification when they click “stop tracking” in my application? Do I have to say something to NotifyManager? I'm basically trying to get the functionality that is in music players, since the “play” notification appears when the user presses the play button, but when they pause, the current “play” notification is destroyed.

In addition, I had never worked with GPS before, but should I do this in the Service so that the user does not stop tracking if the OS takes my application from memory? Or will this not happen?

+5
source share
2 answers

I haven’t done much with notifications, but you can try the following:

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

Replacing NotificationManagerthe name of your instance, of course.

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

Or that:

Notification.Builder.setAutoCancel(true);

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

+9
source

Start

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());

Cancel

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(01);
+3
source

Source: https://habr.com/ru/post/1796572/


All Articles