multiple call to startforeground?

android, service

Solution

Looking at the `Service.startForeground()` source shows that multiple calls to startForeground will only replace the currently shown notification. In fact, the call to startForeground is identical to `stopForeground()`, only with `removeNotification` set always set to true.

If you wish for you service to display a notification for each email in progress, you will have to manage each notification individually from the service.

public final void startForeground(int id, Notification notification) {
    try {
        mActivityManager.setServiceForeground(
                new ComponentName(this, mClassName), mToken, id,
                notification, true);
    } catch (RemoteException ex) {
    }
}

public final void stopForeground(boolean removeNotification) {
    try {
        mActivityManager.setServiceForeground(
                new ComponentName(this, mClassName), mToken, 0, 
                null, removeNotification);
    } catch (RemoteException ex) {
    }
}

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r1/android/app/Service.java#Service.startForeground%28int%2Candroid.app.Notification%29

Problem

i have created a service (EmailService) that sends email ... each time i need to send an email with my app, it starts the service and pass the id of the email via an intent... i am using `startforeground(id_of_email, mynotifcation);` to prevent it from being killed and to show a notification to the user of the status of the email sending. i need to allow the user to send multiple emails at the time, so when the user needs to send another email, it again calls `startservice` with a new intent(different id of email)...so it calls `startforeground(new_id_of_email, mynotifcation);` again. the problem is that the new call to `startforeground` overwrites the previous notification... (so the user loses the previous notification and doesn't know what is going on with his previous email)

Original source