Text update in Notification

Asked

Viewed 60 times

0

I need to update a text in the notification bar but I can’t keep calling the same notification because in the tests I did when I call again it flashes my notification.

I have an app that picks up the gps coordinates and would like as I’m walking it to show me these coordinates in the Notification. In the method below is where I can get all the coordinates, I would like to show Latitude and Longitude in the notification bar.

@Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub

                try {
                    lati = location.getLatitude();
                    longi = location.getLongitude();
                    altitude = location.getAltitude();
                    precisao = location.getAccuracy();
                    time = location.getTime();
                    velocidade = location.getSpeed();

                    editTextVelo.setText(velocidade+" KM");

                    //Preciso chamar Notificação aqui dentro atualizando sempre que houver uma mudança de localização.



                } catch (Exception e) {
                    // progDailog.dismiss();
                    // Toast.makeText(getApplicationContext(),"Unable to get Location"
                    // , Toast.LENGTH_LONG).show();
                }

    }
  • Put the code of the test you did, from it will be easier to help you.

  • @ramaral put, I need to keep updating the notification bar whenever I change the location. There is no way I keep calling the notification because it keeps blinking, I just want to update the text within the notification.

1 answer

0

The way to update a notification is to always use the same id when calling NotificationManager.notify().
If the "blink" refers to the small icon that appears when the notification is launched, use NotificationManager.setOnlyAlertOnce(true) so that he appears only the first time.

Create two methods, one to create the notification and one to make your own update:

//Notificação id
private int mNotificationId = 1;

//NotificationBuilder
private NotificationCompat.Builder mBuilder;

//NotificationManager
private NotificationManager mNotificationManager;

private void createNotificatio(){
    mBuilder = new NotificationCompat.Builder(this);
    mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    mBuilder.setSmallIcon(R.drawable.icon)
            .setContentTitle("Localização")
            .setOnlyAlertOnce(true);
}

private updateNotification(String contentText) {
    mBuilder.setContentText(contentText);

    mNotificationManager.notify(mNotificationId, mBuilder.build());
}

Call the method a first time createNotification(), for example in the onCreate().
In the method onLocationChanged(), compose a string with latitude and longitude and call the method updateNotification() with her.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.