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.
Related
I am just clearing up my Android doubts. So lets talk about Services, we use services when we have to perform an operation in the background. Lets say Playing Music in the background? Thats one of the most popular reasons why people use Services.
But i just used Mediaplayer in my Mainactivity and i used .start() method in onCreate of my activity. I then minimized the app, but guess what....music still plays. I open the app again, and the music is still playing. I then remove the app from recent apps, basically destroying it and the music stops. Basically the app performs how it is supposed to. Works PERFECT!
Then why do we use services at all for these kind of tasks?
Is there any scenario this code might create a problem? Or is this creating some memory leak or something?
When "minimized" the App continues to play music due to OS delay which allow an User to re-open the App without delay (due to a closure mistake?) because all remained in memory (even the Music) in the previous state.
But the memory is limited, and the CPU the same, so the OS could kill or freeze (using Doze) a background Activity in any moment without prompt User/Developer.
An Activity could be killed to claim Memory or freezed to save CPU cycles. In this situation only explicitely declared Background Services are allowed to run (however with some limitations and special-behaviours), so if you don't use Service your Music could stop in any moment in the future.
PS: even Background Service could be killed from OS, but this case is very-very rare and only on Extreme Low Memory situations.
It will play as long as Android lets it live. It may stop at any moment, usually after 10-20 minutes.
Consider using Foreground Service to playback music. It will last until user kills it, and also provides notification bar with control over the playback.
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
I created a music app on the android platform but when I let it run for a few minutes when the screen turned off, the app turned off and didn't play the music anymore. So how do I extend the time the application runs when the screen is turned off?
I'm assuming that you are running the Service to play music in the background. So, when OS thinks that the mobile device has to reduce the battery consumption, it just kills your service sometime after it was run.
Now, what you can do to avoid this, is running that in foreground instead. Because then that service is kind of marked as useful by the user so that doesn't get killed unless the user themselves do that.
Find out more about How to set a Service to run in foreground here in the documentation
The above link clearly mentions:
a music player that plays music from a service should be set to run in the foreground
But keep in mind that any service to accomplish any task should run in the foreground only if the user is aware that the task is being accomplished.
You should only use a foreground service when your app needs to perform a task that is noticeable by the user even when they're not directly interacting with the app.
And for that reason, you must show a notification mentioning that this service is being run in the foreground, to accomplish this task, so that the user can be in knowledge of that.
A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
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.
I have an android application that starts a server NanoHTTPD. The problem happens when I start the server, I leave the application in the background, and I start playing a game or a heavy application. After a few minutes the server is stopped. How I can keep the server started, even playing games or using other applications? (like AirDroid)
Thanks in advance.
It'll get killed by the Android system unless you place it into a Service and make it a foreground service.