Text Update in Notification

0

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

I have an app that takes the coordinates of the GPS and I would like as I'm walking it shows me these coordinates in the Notification. In the method below is where I 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();
                }

    }
    
asked by anonymous 10.11.2015 / 20:29

1 answer

0

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

Create two methods, one to create the notification and another to make your 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());
}

First call the createNotification() method, for example in onCreate() . In the onLocationChanged() method, compose a string with latitude and longitude and call the updateNotification() method with it.

    
10.11.2015 / 22:05