How to Stop an Android Bound Service when the app is killed? - android

I have created a Service to implement a Music Player functionality and i am binding to the service.
Everything is working fine, the only problem is that when I kill my app while the song is playing, it kills the app, the notification also goes away but the song keeps on playing in the background without any notification, and to stop the audio, the app needs to be launched again.
But when i clear all tasks (kill all the apps) while the while a song is playing, it kills the app as well as the Song, which is correct.
I need a solution to stop the Audio when only my App is killed.
I have implemented onTaskRemoved() callback but the control doesn't always come there.
Can anyone help me here ? Thanks in advance.

Seems like the app is not disconnected fully from the service,
Try to call unbindService().
If your client is still bound to a service when your app destroys the client, destruction causes the client to unbind. It is better to practice to unbind the client as soon as it is done interacting with the service. Doing so allows the idle service to shut down.
Try looking at Managing the lifecycle of a bound service
https://developer.android.com/guide/components/bound-services#Lifecycle

Related

Android app and its service being killed after open some applications

I'm developing an app to track the user location when they hit the button Start. I implemented a service to record the location with the LocationListener and it works well but I have being done some heavy testing and when I open some applications my app and service are getting killed by android randomly.
But I downloaded an app called Wikiloc and doing the same heavy test, opening multiple apps this app is never getting killed and I see the app creates a notification that can't be dismissable. Can it be related to the fact that the app is never killed by the system?
How can acheive this in my app? Do I have to do the notification trick? If so, how it is implemented?
Take a look at the documentation for the Android Service's startForeground(int, Notification) here. According to the documentation:
Make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state. By default services are background, meaning that if the system needs to kill them to reclaim more memory (such as to display a large page in a web browser), they can be killed without too much harm. You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing.
You call startForeground inside your Service's onCreate method.

How to proceed playing music after app have been killed?

After android appication have been killed, all the inner threads are stoped. How to proceed playing music using service and threads? Should I keep time of music in killing time and re-play from that exact time?
EDIT
I do use service and it plays in backgound. The only thing I want it to play when the app have been killed by user, in other words the music must be contolled only via notification buttons and not be related to application lifecycle
Make your service a Foreground Service. Then it will run even after the app is killed.

Keep a Service started at BOOT running even if Application stopped

I am working on a app that during boot time starts an activity that logs in to my server (needs an activity to log in through facebook) using a service (initiated with startService). This service establishes XMPP listeners and does nothing after that, just waits for connection. I want this service to run all the time the device is up.
The problem is that the activity stops after a while and my service is also stopped. The service returns START_STICKY so I was expecting it to hang around. Also the service doesn't do anything except wait for connection.
The activity has the properties:
android:excludeFromRecents="true"
android:noHistory="true"
android:launchMode="singleInstance"
so that it does not show up in the task list (when user long presses the home button).
The activity is stopped when the user long presses the home button and the service also ends. I am thinking its possible that the application exited, that's why the service also ends. I could not find any way to keep the activity from not stopping. Maybe its stopping because of the above properties.
So what can I do to keep the service running all the time. How can I keep the application from being removed. I read somewhere that if I keep a while loop running in the service then START_STICKY can keep the service around??
I can use AlarmManager to start the service but I don't want it to stop easily and then have to restart it every time.
I don't want to run a foreground service. I can not run the service in a different process since I am using existing code that does not do IPC. Any help will be appreciated. Thanks.
There are two things to keep running a service indefinitely; create the service using startService() and return START_STICKY from onStartCommand(). You seem to be doing this both. With these two steps, the service may be shut down by the system but it should restart almost immediately.
The only suggestion I have is to create a separate thread in the service. This is because by default, started services run in the application main thread. If the service is constantly doing certain task, it may block the main thread and kill the application. Google doc has an example of implementing this:
http://developer.android.com/guide/components/services.html#ExtendingService

Android: UI state dependent on serice (music player scenario)

I have an app which starts a service (using startService). This service performs something similar to playing music. It can be seen as an indefinite work which has to be stopped manually through my activity.
In my activity, I would like to use a "switch" to show the state of the service (running/not running). This is somewhat like a play/pause button on a music player.
When my activity is created, how would I create the UI (switch) to be consistent with the service state (running/not running)?
I don't this using saveInstanceState/restore... will work. My app could be killed completely and the service will service and I will not receive the instance state once started again.
I don't this using SharedPreferences/DB will work. My process could have been killed and at next start, the app would think that the service is running.
The only stable solution I've been able to come up with is to ask the service (maybe through binding) if it is doing work or not.
Would this work? How would you do this?
Binding to the service would work (we have this exact scenario with a background audio player and resolved it that way).
Be mindful that binding is asynchronous, though.

Control service in GUI

I am writing an app that uses a media player. I want to start the media player using the service so that the music can be played even if the application is closed (onDestroy is run). Should I use BindService (for control the service) and not unBinding that?
the life cycle of the Service would be a bit tricky, start at playing screen created and stopped when either the music is complete in background or music is stopped when the app is finished (onDestroy)
how should i implement my service to best fit the above case i need?
Should I use BindService (for control the service) and not unBinding that?
No. You should call startService() to start it and stopService() to stop it (e.g., when the user presses the Stop button).

Categories

Resources