I was trying to create a service that can interact with the AppUI when the App is opened and continues its working when the App is closed.
I have heard that the bindService() call creates a Bound Service that can communicate with the App. At the same time, Some resources say that the startService() can be further made a Foreground Service by calling startForeground() inside the onStartCommand() callback, and then use them to communicate with the App.
Can anyone give the best way to achieve my need and the difference between this?
From the Official Documentation: https://developer.android.com/guide/components/services.html
Bound
A service is bound when an application component binds to it by
calling bindService(). A bound service offers a client-server
interface that allows components to interact with the service, send
requests, receive results, and even do so across processes with
interprocess communication (IPC). A bound service runs only as long as
another application component is bound to it. Multiple components can
bind to the service at once, but when all of them unbind, the service
is destroyed.
Foreground
A foreground service performs some operation that is noticeable to the
user. For example, an audio app would use a foreground service to play
an audio track. Foreground services must display a status bar icon.
Foreground services continue running even when the user isn't
interacting with the app.
It looks like you should use Foreground Service.
Related
When I used foreground service then when I kill the application then foreground service is automatically killed but I want to alive foreground service when the application is killed. this issue appeared in android 10 and android 11. how to solve this issue.
You can't do that as you should not be able to keep alive a foreground service after the application is killed.
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. If the action is of low enough importance that you want to use a minimum-priority notification, create a background task instead.
If you what to have some background service working, that will be possible.
You can find some useful information here :
https://developer.android.com/guide/components/foreground-services
https://developer.android.com/training/run-background-service/create-service
It should not get killed. When starting foreground service you need to create notification too. You have done that, right?
If you done everything right there is possibility that you have xiaomi phone. Xiaomi deletes everything when app killed. You need to add specific intent protection...
How to handle background services with Android Oreo?
i had created intentservices and services background but due to android Oreo limitation the services stopped when the app closed
WhatsApp app works in background on android 8.1 how is this possible ?
Check this image:
Also, the broadcast receiver is not working when the app is closed
App in Foreground - Best option is to use service or Intent Service if you need to some task when application is open or in the stack.
App in Background - If you want to perform some long running periodic operation then best option is to use Jobservice running in background and Jobscheduler implementation to schedule that jobservice. Android O and above JobScheduler is recommended for background operations.
Android background services are described in the Android documentation. Google is trying to limit the freedom apps have to run services in the background for security reasons and to save battery.
You basically have the following options:
1. Foreground Service
A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track.
2. Background Service
background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.
If your app targets API level 26 or higher, the system imposes restrictions on running background services when the app itself isn't in the foreground. In most cases like this, your app should use a scheduled job instead.
3. Bound Service
A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it.
I have been reading Android docs and I feel I am bit lost and confused.
What is the type of service I need to use in Android, so that I can keep running my code even when the app is paused or minimized for prolonged period.
I am not interested in running the service if the app is closed. I want to run small piece of code that will run when app is in foreground or background, but not killed.
You can use a bound Service. The Service will stop after all the bound clients disconnect. Your Activity binds to the Service and when your Activity is killed or finished, you unbind and the Service stops. If Android kills off the Activity, the bound connection is also shut down and your Service will stop.
I've decided to use a service for the Android Studio app I'm working on; however, I can't seem to find a way to reference the service in my project. The service will enable the mic to continuously record and using a thread, notify the user if the sound is over a certain loudness and then update the phone's location accordingly. So within the service, if the mic listens into something above a certain loudness threshold it will pass the information to the Google Maps activity. The service is a started service that starts after a button in another activity is pressed.
Is there a way to reference the service, possibly a variable within the service, from my Google Maps activity java code? Do I have to use a bounded service instead?
You describing a service that should have connection to ui, notify it about it state and possibly allow changing it. You should you use bounded service for that, they have exactly what you need: https://developer.android.com/guide/components/bound-services
I have a pool of AsyncTasks which pass state data back to the service that executed them, but they keep being killed by Android.
I can't use an IntentService because the Service is keeping track of State used by UI and AsyncTasks.
How do I:
1) keep the service from being killed
2) Replace either service or AsyncTask with something better for this use case?
You should use foreground service. Android OS can kill service if it is running short on resources, but it will always spare foreground service.
The Android system will force-stop a service only when memory is low
and it must recover system resources for the activity that has user
focus. If the service is bound to an activity that has user focus,
then it's less likely to be killed, and if the service is declared to
run in the foreground (discussed later), then it will almost never be
killed.
Note: Another thing you should also consider
Caution: A service runs in the main thread of its hosting process—the
service does not create its own thread and does not run in a separate
process (unless you specify otherwise). This means that, if your
service is going to do any CPU intensive work or blocking operations
(such as MP3 playback or networking), you should create a new thread
within the service to do that work. By using a separate thread, you
will reduce the risk of Application Not Responding (ANR) errors and
the application's main thread can remain dedicated to user interaction
with your activities.