Know Android Services better!

What is a Service?

What does that mean?

Thread or Service?

Types of services:

  1. Started service or Background service(starts by calling startService())
  2. Foreground service(starts by calling startForeground())
  3. Bound service(starts by calling bindService())

What to use when?

  1. Background service is used when your operation is not noticed by the user. For example if you are syncing your data with the server, a background service is recommended.
  2. Foreground service is used when your operation should be noticed by the user. For example playing music or downloading a file.
  3. Bound service is used when your operation is related to interprocess communication. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). In simple words, when you want to get results continuously from your service you should use bound service.

How to use?

public class LocationService extends Service {
..
..
..
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: ");
..
..//Do one time setup here
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null; //Used for bind service
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Called when you startService() is invoked
return super.onStartCommand(intent, flags, startId);
}/*
To use foreground service, just call foregroundService() and passing it a notification id and notification. To stop foreground service call stopForeground(true)
*/
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy: ");
super.onDestroy();
//Called when stopService() or stopSelf() is invoked
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.i(TAG, "onTaskRemoved: ");
super.onTaskRemoved(rootIntent);
//Called when you remove your app from recents screen while your service is active
}

IntentService, an easy way:

class GeofenceIntentService : IntentService("GeofenceIntentService") {

val TAG = "GeofenceIntentService"

override fun
onHandleIntent(p0: Intent?) {
val geofenceIntent = GeofencingEvent.fromIntent(p0)
Log.i(TAG, "onHandleIntent")
if (geofenceIntent.hasError()) {
val errorMessage = geofenceIntent.errorCode
Log.i(TAG, errorMessage.toString())
return
}

val geofenceTransition = geofenceIntent.geofenceTransition
var type = ""
when
(geofenceTransition){
Geofence.GEOFENCE_TRANSITION_EXIT -> type = MainActivity.EXIT_GEOFENCE_STRING
Geofence.GEOFENCE_TRANSITION_ENTER -> type = MainActivity.ENTER_GEOFENCE_STRING
Geofence.GEOFENCE_TRANSITION_DWELL -> type = MainActivity.DWELL_GEOFENCE_STRING
else
-> type = "Nothing"
}
LocalBroadcastManager.getInstance(this) .sendBroadcast(Intent("transition_broadcast").putExtra("action", type))

}
}

--

--

--

Android Developer at ShareChat

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Deploy API Gateway, Lambda and DynamoDB using Terraform

How To Create A Circular Progress Bar using HTML CSS & Simple JavaScript

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

API Observability with Apache APISIX Plugins

Will Julia Programming Language Ever Rule the World? The ultimate Revolution

Asynchronous Programming with Flutter Streams

How to write better technical content

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kalpesh Chandora

Kalpesh Chandora

Android Developer at ShareChat

More from Medium

Some Best Practices for Android App Architecture

Android Custom Loader

Asynchronous Android : Kotlin Coroutines

Android WebView The Missing guide