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.
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...
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.
What I have studied on stackoverflow and Android documentation.
Finally I've concluded this:
There is no way to create a background service for continuous tasks. If I really want a service I should start a foreground service and user continuously sees a persistent notification "App is running". There is no way to hide this notification. It is intentionally added by Google.
Yes there are other options like WorkManager and JobScheduler but they do work periodically not continuously.
What I do want is to build an instant messaging app which continuously connects to the server using xmpp or sockets. But it requires a continuous connection but I don’t want to use a foreground service because it shows an irritating notification to the user "App is running".
Question 1: How does Whatsapp and other instant messaging app continuously connect to the server but not show a persistent notification ? How do they achieve this ?
Question 2: If Whatsapp use FCM for notifications then it will also work in those mobile which do not have playservices installed, so how does Whatsapp notification mechanism works ?
Starting with Android 6.0 (API level 23), Android introduces two power-saving features that extend battery life for users: DOZE and APP STANDBY. These two features enforce many restrictions on your background processing while the phone is in Doze mode. You should read about Doze and app standby in the following link
https://developer.android.com/training/monitoring-device-state/doze-standby
Now, about your use case is that you want to receive the messages and incoming calls even when the app is not running. For this use case, Android announced High Priority FCM messages in GoogleIO2016. They are high priority Push message which grant the application temporary wakelock and network access, independent of Device's Doze state or if the app happens to be in the app standby. This allows the application to react to the message and notify the user in whatever way it wants about the instant message or incoming call.
I don't know exactly how WhatsApp does that unless I look at their code but you can handle your use case using FCM High Priority Messages.
For more about your use case, follow the below link ofGoogleIO2016 Video from 08:30m to 10:30m
https://www.youtube.com/watch?v=VC2Hlb22mZM&t=505s
and read about this use case on the first link in this answer.
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.
So I am sending post request to mysql database through a handler and this handler is posting it through a runnable to a async task Now I want to send this request even if my app is not active so that i can notify the user through notifications When my app is running everything is working fine but when i press the home button or close the app it stops so how can i prevent the handler from stopping its processes even when the app is not running
Use Android Service
Android service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn't has any UI (user interface).
The service runs in the background indefinitely even if application is destroyed.
See:
http://www.javatpoint.com/android-service-tutorial
https://developer.android.com/guide/components/services.html
If you want your user to get some notification messages when new data is available use
Firebase Cloud Messaging(https://firebase.google.com/docs/cloud-messaging/)
or
Google Cloud Messaging(https://developers.google.com/cloud-messaging/)