Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Service Multiple Activities in Android

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()); 
 service.putExtra("dataToSend",data.toString());
 startService(service);

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.

like image 320
Elior Avatar asked Aug 05 '15 10:08

Elior


People also ask

Can a service be bind with multiple components in Android?

You can connect multiple clients to a service simultaneously. However, the system caches the IBinder service communication channel. In other words, the system calls the service's onBind() method to generate the IBinder only when the first client binds.

How pass data from one activity to multiple activity in Android?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How activities are different from the services in Android?

Services are a unique component in Android that allows an application to run in the background to execute long-running operation activities, on the other hand, an activity, like a window or a frame in Java, represents a single screen with a user interface.

Is responsible for binding two activities?

In Android, seeing as only one Activity can bind to a Service at a time, and only one Activity can be shown at a time, there is no real reason to want to bind 2 Activities at a time. But, if you'd like, the best solution is to bind the Service in the onResume() method, and unbind it in the onPause() method.


2 Answers

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

@Override
protected void onStart() {
    super.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 
    }
};
like image 68
Arun Antoney Avatar answered Oct 13 '22 01:10

Arun Antoney


QUESTION
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.

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.

PROBLEM

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:

onStartCommand

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.

startService

Request that a given application service be started.

SOLUTION
call 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.

like image 28
Jordi Castilla Avatar answered Oct 13 '22 01:10

Jordi Castilla