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.
Related
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 a player app.
For this reason, it uses a foreground service to handle the playback.
Until recently the service was bound to my activities.
This is not the case anymore.
Since then, some specific devices (mostly Pixel 1/2/3) have been killing my app 1 minute after the screen has been turned off
The service is a foreground service not bound to anything.
Why would the device kill it?
As soon as the app is excluded from the device-optimized apps list the issue is solved
I'm not providing code, because I'm just trying to understand if this situation makes sense and if so what should I do to prevent this
BTW the app is using a receiver to act on Screen_ON/OFF messages. That's how I can see in the logs that the player service onDestroy() method gets killed exactly 1 minute after the screen has been turned off
what should I do to prevent this?
The key point here to keep the service alive is as said in official documentation :
While an app is in the foreground, it can create and run both
foreground and background services freely.
so, we can conclude that keeping the work in foreground and visible to the user has very minimal chances of being killed. And to do so we need to know that how android gets the idea that this process is in foreground ?
Here are the criteria's at which a process is said to be in foreground:
It has a visible activity, whether the activity is started or
paused.
It has a foreground service.
Another foreground app is connected to the app, either by binding to
one of its services or by making use of one of its content
providers. For example, the app is in the foreground if another app
binds to its:
-IME Wallpaper service
-Notification listener
-Voice or text service
If none of those conditions is true, the app is considered to be in
the background.
If none of the above criteria is fulfilled by your app process then thats the reason of your service being killed.
You can read more on this topic here :
Foreground service being killed by Android
I have an application which once the user runs the app it has a long process to run, such as downloading needed files and setting it all up and what not. I would like to move the process to the background and show a notification in the status bar while the process is running.
What would my best strategy be to keep the process alive while downloading the files and what not. I've read about services, but have also heard they're easily killed? Should I use a service or should I just run a thread with maximum priority and just throw a notification up and close it when the process is over?
What's best thank you for any help. Process by the way is about ten minutes no longer than it takes to download a rom in rom manager basically just want that same setup thank you for any help.
It sounds like a Foreground Service is exactly what you need. When you create it, a notification is placed in the status bar to tell the user that it is running. The service is also given high priority so that it will not be canceled.
From the documentation for the Foreground flag:
public final void startForeground (int id, Notification notification)
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 can look at the rest of the documentation here.
A service is exactly what you want. You can specify how high of priority your service is when you create it (see #theisenp's post).
As for moving your Activity to the background and reopening it later - I recommend starting the service, then actually stopping your app using the finish() method. Then, once the service is complete, relaunch your activity with Intent, or broadcast an Intent that can be handled by a BroadcastReceiver.
What are the disadvantages of running services in Android in Foreground.??
I recently read that if you want your services to last longer and not get killed easily we need to run the service in foreground.
What you read is right. It depends on what you want to do with your application. If your service does something which should not be interrupted without explicit user-interaction you should start it as a foreground-service. This assures that the service won't get killed if more memory is needed by other applications. Also you have an ongoing notification displayed so that the user is aware of what is happening and you can provide functionality to your notification such as opening an activity when tapping on the notification etc. Examples for this may be a music player service or a download service. If you have a service which does not have to run necessarily after leaving the app you should choose a service started in background, so that the memory can be released if it is needed for other tasks. Some more information you can find here: http://developer.android.com/reference/android/app/Service.html
I am currently writing my first Android application and I keep running into references to background and foreground services. Since I intend on using a service in my application I was hoping to get a clarification between the two and how they are used.
Perhaps this will answer your question:
A started service can use the startForeground 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. 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.
More info can be found here
Foreground: The process relies on onPause() and onResume()...i.e you play music player and pressing pause and play
Background: The process which runs without user interaction i.e receiving a message, incoming call, receiving mails, or setting alarms. The method used here is onStart() and onStop().
For example, check it on your phone. Create an alarm at 6:30am. When the system clock reaches 6:30am it fires. In order to kill the alarm service, just go to menu-->settings-->application-->Running service-->click stop service. It stops the alarm service even when your system reaches the time it won't fire.
Foreground Service is used when User is interaction with application and when Service is doing something visible to user. Background Service is used when even user close application (discard from recents) and when Service is doing something not visible to user like downloading data from server, load data from a ContentProvider etc.. And Foreground Service is less likely to be killed by system on low memory.