Pergunta

I am developing for Honeycomb and for days i am trying to solve this problem. I have an notification service without intent (don`t need one), the problem is that after every call for displaymessage function the notification pup-up each time, so i get 100 notifications. I would like it to popup only once and after that only change the text of percent. Similar to downloading from market progress bar and percentage. I have isolated the function and created new testing code but with no success. If you look at this from other angle, i wish to change the text on existing notification without creating new notification. Can you please help me?

Here is the whole code (after the isolation):

package com.disp;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;

public class DispalyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        for (int i = 0; i < 100; i++) {
            SystemClock.sleep(300);
            displaymessage(""+i+"%");

        }
    }

    public void displaymessage(String string) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        Notification notification = new Notification(R.drawable.ic_launcher, "Notification Service", System.currentTimeMillis());
        Context context = getApplicationContext();
        notification.setLatestEventInfo(context, "Downloading Content:", string, null);
        final int HELLO_ID = 2;
        mNotificationManager.notify(HELLO_ID, notification);

    }
}
Foi útil?

Solução

Because each notification is uniquely identified by the NotificationManager with an integer ID, you can revise the notification by calling setLatestEventInfo() with new values, change some field values of the notification, and then call notify() again.

You can revise each property with the object member fields (except for the Context and the notification title and text). You should always revise the text message when you update the notification by calling setLatestEventInfo() with new values for contentTitle and contentText. Then call notify() to update the notification. (Of course, if you've created a custom notification layout, then updating these title and text values has no effect.)

from

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top