Android background service is an android component that runs in the background. There is no GUI for users to interact with the android background service object directly, it is usually started in android activity and runs in the same thread of the activity. When users quit the android-activity, the android background service will also exit …
Android foreground service is an android service object. It always runs in the foreground, this can avoid service objects being recycled by the android system when android os does not have enough resources. Android foreground service can interact with users through notification. It is usually used in a music player, file downloader, etc which user …
When you start an android service in activity, the service runs in the activity main thread. If the service is time consuming, then the main thread will be blocked. So we had better start a child thread in the service object, this can make your activity execute smoothly.
To start an android background service when the device boots, you should create a broadcast receiver, and make it listen to android.intent.action.BOOT_COMPLETED action. Then in the custom broadcast receiver’s onReceive method, you can start the background service.
This article will show you how to communicate between activity and background service in android. It will play, pause and stop a web audio file in android background service in this example. It will also update activity progress bar from background service to display audio playing process.
This example will show you how to implement force logout when same user account login in another instance of same android app. It will use both android activity, service and broadcast receiver components to implement.
In some cases, you may need to create a broadcast receiver which can still run at the background after the android app exit. This example will just tell you how to do that use broadcast receiver and android service object.