Sobes.tech
Junior — Middle

What methods can be used for communication between services and activities in an application?

sobes.tech AI

Answer from AI

Communication between services and activities in Android can be achieved through several methods:

  • Intent — for starting services or passing data between components.
  • Binder — a mechanism for interacting with a service via an interface, especially for bound services.
  • BroadcastReceiver — for sending and receiving broadcast messages.
  • Messenger — for exchanging messages via Handler.
  • LiveData or other architectural components — for observing data changes.

For example, an activity can start a service using an Intent and pass data:

Intent intent = new Intent(this, MyService.class);
intent.putExtra("key", "value");
startService(intent);

Or connect to a service via Binder to call methods directly.