One Service Multiple Activities in Android

android service
android service onbind
bound service android
android startforegroundservice
android bind multiple activities to one service
android background service example
android run service in background forever
android keep service running after app killed

I have a service and 3 activities. The service is started in the first activity. Now when I move to the second activity and pressing on some button, I want to send a data to the service.

so in the second activity, inside the click listener I did the following:

 Intent service = new Intent(MyService.class.getName()); 

but the method onStartCommand inside the service, doesn't get called..

Also, I want to create this service to work with multiple activities. I mean each activity will be able to send a data to this service and get a data from it.

QUESTIONI want to create this service to work with multiple activities. I mean each activity will be able to send a data to this service and get a data from it.

You don't have to do nothing with your Service. Services remain the same if they are not destroyed, that means all your Activities will be able to access to it's data.


but the method onStartCommand inside the service, doesn't get called..

WHY As long as your service has started, the onStartCommand is not called each time:


Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.


Request that a given application service be started.

SOLUTIONcall the events you need in onRebind method:

Called when new clients have connected to the service, after it had previously been notified that all had disconnected in its onUnbind(Intent). This will only be called if the implementation of onUnbind(Intent) was overridden to return true.

Bound services overview, An Activity is an application component that provides a screen with which users can Console Terms of Service · SDK License Agreement The savedInstanceState Bundle is same as the one used in onCreate(). component allows you to reuse the component across multiple activities without having to duplicate code. Multiple Activities. Even the simplest applications have more than one functionality. Hence, there is often a need to deal with multiple activities. For example, a game can have two activities: a high scores screen and a game screen. A notepad can have three activities: view a list of notes, read a selected note, and edit a selected or new note.

Hi you need to bind your running service to activity you need to access it again. Following code snippets you can used as a reference

protected void onStart() {
    Intent mIntent = new Intent(this, MyService.class);
    bindService(mIntent, mConnection, BIND_AUTO_CREATE);

ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceDisconnected(ComponentName name) {
        mBounded = false;
        myService= null;

    public void onServiceConnected(ComponentName name, IBinder service) {
        // Toast.makeText(context, "Service is connected", 1000).show();

        mBounded = true;
        LocalBinder mLocalBinder = (LocalBinder) service;
        myService= mLocalBinder.getServerInstance();

      //now you are able to access any method in service class 

Understand the Activity Lifecycle, startService() do not nest (though they do result in multiple corresponding calls to components running in the same process as the service (such as an Activity )  Suppose in your Android application there is a class that you need to use in every Android Activity. For example, you use a progress bar in your application and the progress bar will be the same for all the classes to show functionality. So this article explains how to use a single class in multiple Activities.

Hey call your service with below intent

 Intent mIntent = new Intent(this, MyService.class);



in your case you are passing service class name and that will become action to your intent. So service won't start.

onstartCommand will call every time when you call startService and you can get data from intent which passed as extra.

To receive data from service to your activity. you can broadcast data from service and register broadcast in your activities


you can use binding mechanism

onStartCommand, Each activity displays a user interface that allows the user to perform a To take the user from one activity to another, your app must use an  The Android system then calls the service's onBind () method, which returns an IBinder for interacting with the service. The binding is asynchronous, and bindService () returns immediately without returning the IBinder to the client. To receive the IBinder, the client must create an instance of ServiceConnection

this what i did to make different activities to send and receive data from service

 public class SensorsActivity extends AppCompatActivity {
BluetoothService mService;
boolean mBound = false;

String valueReceived;
protected void onStart() {
    Log.v("STATE", "onStart() is called");
    // Bind to BluetoothService
    if (!mBound) {
        Intent intent = new Intent(this, BluetoothService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

protected void onCreate(Bundle savedInstanceState) {

            new IntentFilter("in.purelogic.simsonite.REQUEST_PROCESSED"));

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className,
                                   IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        BluetoothService.LocalBinder binder = (BluetoothService.LocalBinder) service;
        mService = binder.getService();
        mBound = true;

    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;

//BroadCastReceiver to let the MainActivity know that there's message has been recevied
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        try {
        } catch (Exception e) {

//The Message that comes from the service
private void handleMessage(Intent msg) throws Exception {
    Bundle data = msg.getExtras();
    valueReceived = data.getString("DATA");
    Log.e("recv2", valueReceived);



Interacting with Other Apps, If you create a Thread or an Executor in an Activity of your app, this A lot has changed with background Services in Android O. One of the main Even if you start a Service multiple times, it will only call onCreate() once  <activity android:name="Leeder" to: <activity android:name="LeederTitleScreen" If you want your title screen to start the game via startActivity(), you'll also need to declare your Leeder activity in the manifest. Edit: Yes, you need the <intent-filter> section. It tells the system which implicit intents your activity will respond to.

one possible solution but not the best is using sharedpreferences . you can use it as a massage Transmitter from your activity to your service.

Deep Dive into Android Services, Android is one of the most popular operating systems for the mobile. I am sure that basically one Application contains multiple activities and lots  Android application is composed of multiple activities and they often communicate with each other by passing data. For example, lets say your application contains two activity Activity1 and Activity2. Activity1 is marked as launcher activity, which means it will be invoked automatically when user taps on the application icon from applications list. Note that, there can be at most one launcher activity throughout application.

How To Work With Multiple Activities And Navigate The Activities In , Android · service. Hi , I have created a service , i want to read data fro it in multiple activities how i can do. anyone please provide me an  xamarin android, xamarin activity, xamarin change activity, xamarin android activity, xamarin new activity, xamarin activities, xamarin android activities, xamarin change page, xamarin switch page

how to bind service to multiple activities, A bound service allows components (such as activities) to bind to the service, send Also, if multiple activities in your application bind to the same service and​  In this video we will learn, how to open a new Activity from a Button click. For this we will create a new Intent and pass it to the startActivity method. Ex

Bound Services, The main purpose of this post is to provide the idea of different android constructs which can be used for communication between activity and service. If application and service are running in same process which is mostly the case then  As outlined in some detail in the previous chapters, bound services, unlike started services, provide a mechanism for implementing communication between an Android service and one or more client components.

  • Show the service code
  • To get data I would recommend EventBus as it is by far the easiest method that I have found compared to binding to the activity or sending broadcasts (assuming that the service emits data). If you need on-demand access to data you will probably have to bind to the service.
  • instead of using Start/Stop schema, use Bind/Unbind to that Service
  • hi thanks, but if you'll notice.. i do call to startService with intent.. as i understand, once startService is getting called, the onStartCommand should be also get called. no?
  • if you take a look to links in the answer you will nottice startService is a Context method, so onStartCommand is not being called... It's important to understand lifecycle in Android apps to know what and why is happening
  • ok but if I call this method by using this: "getApplicationContext().start..." it doesn't work also
  • Service does not have this method, you're already calling it by startService(service);, please read the documentation:…
  • ok I understand. I'll explain again what i'm trying to do: lets say I have 2 activities,activity A starts the service and now I'm moving to activity B. In activity B I have a button, when this button is clicked by the user, I want to send a data to the service. I did it by calling again to startService with an intent as a parameter. the doc says that when you call to startService the method 'onStartCommand' get called also