#StackBounty: #android #android-service #android-lifecycle #android-service-binding #android-application-class How do I enforce Applica…

Bounty: 50

Basically I’ve got an application that runs a foreground service.
When I start the application I need to do session specific initialization in the Application’s onCreate method.

When I close the app, the service keeps running (desired behaviour), however, when I reopen the app from the launcher / from my notification, the Application onCreate isn’t getting called again.

My question are:

  1. How do I enforce the Application onCreate be called again even though there’s a service running? (The service is probably keeping a reference to the application object, right?)
  2. Is there a way to get indication inside the Application class that the app has been started again but from a service?
  3. What other solutions can you think about?

MyService in AndroidManifest.xml:

<service android:name=".MyService"
    android:exported="false"/>

My service’s onStartCommand:

    createNotificationChannel();
    Intent notificationIntent = new Intent(this, MyActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("My Service")
            .setContentText("Service")
            .setSmallIcon(R.drawable.ic_android)
            .setContentIntent(pendingIntent)
            .build();
    startForeground(1, notification);


Get this bounty!!!

#StackBounty: #android #android-service #android-notifications #foreground-service How to make notification dismissible when playing mu…

Bounty: 50

Spotify and Google Music allows a user to play music in background allowing also the media notification to be dismissible.
When you move one of these apps to background, then pause the playback, you are able to dismiss the media notification. However if you don’t dismiss the notification, so it is left in paused state for long time, the System won’t kill the “audio service” as the notification is always visible and immediately react to play/pause.

As far as I know it is not possible using foreground services as in order to dismiss the notification, we’d need to call stopForeground(false), which would let system to kill the service.

Here is how we do it right now, in our app:

When playback is started, we run MusicService as a foreground service with following code:

startForeground(mediaNotification)

Then, when user leave the app, he is able to control the playback via media notification. As the notification is tightly coupled with the MusicService, user won’t be able to dismiss it until we call:

stopForeground(false)

That’s why we call this method when user press pause button.
However this makes a Service background, so system will kill it in short period of time.

How Spotify and Google Music could workaround it? What is the way to achieve it?


Get this bounty!!!