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)
After the system creates the service, the app has 5 seconds to call the service's startForeground() method to display the user-visible notification for the new service. If the app does not call startForeground() within this time limit, the system will stop the service and declare the application as ANR.
A started service can use the startForeground(int, android. app. Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory.
Starting in Android 13 (API level 33), users can dismiss the notification associated with a foreground service by default. To do so, users perform a swipe gesture on the notification.
Foreground services are an advanced Android concept which allows you to display notifications to your users when running long lived background tasks. The notification acts like any other notification, however it cannot be removed by the user and lives for the duration of the service.
// Build the intent for the service Inside the service, usually in onStartCommand (), you can request that your service run in the foreground. To do so, call startForeground (). This method takes two parameters: a positive integer that uniquely identifies the notification in the status bar and the Notification object itself.
Apps that target Android 12 (API level 31) or higher can't start foreground services while running in the background, except for a few special cases.
Note: Although adding a foreground service type gives a foreground service the capability to access location, the camera, or the microphone, this foreground service is still affected by the access restrictions that were introduced in Android 11.
If your service is started (running through Context#startService (Intent) ), then also make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With