0

In android i am using two services and i want to move some details from one to another service. Becouse of this i create new class Settings with set and get methods but i don't know how to connect two services with this one class file that i can move details from one service to another and remember each one.

for example:

i want to transfer property Boolean from service 1 to service 2 and then in service 2 check if this property is true and if is true then i execute some code in this seocnd service... hope is better explanation

class example:

public class Settings {
    private int currentAudioManager;
    private Boolean isChanged;

    public int getCurrentAudioManager() {
        return currentAudioManager;
    }

    public void setCurrentAudioManager(int currentAudioManager) {
        this.currentAudioManager = currentAudioManager;
    }
........

Hope you understand what i want.

senzacionale
  • 20,448
  • 67
  • 204
  • 316

1 Answers1

1

You can register broadcast receiver and send broadcasts parceable data between your services.

public class Service1 extends Service {

    @Override
    public void onCreate() {    
        super.onCreate();
        registerReceiver(recevier1,  new IntentFilter("YOUR_SERVICE_ACTION1")); 

        // if something happen in your service"
        sendBroadcast(new Intent("YOUR_SERVICE_ACTION2")); // send to second service
    }

    @Override
    public IBinder onBind(Intent intent) {      
        return null;
    }

    public void onDestroy() {
        unregisterReceiver(recevier1);
    };

    Service1Receiver recevier1 = new Service1Receiver();    
    private class Service1Receiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {     
            intent.getExtra("me.SERVICE");// handle your data
        }
    }

}
Yahor10
  • 2,123
  • 1
  • 13
  • 13
  • thank you for idea but inside one service i need to check if second service done already and move this details to second service. CAn i do this with receiver? – senzacionale Aug 16 '12 at 13:19
  • You can use IBinder for local services to get state (as case) or read this ref http://stackoverflow.com/questions/600207/android-check-if-a-service-is-running about variables – Yahor10 Aug 16 '12 at 13:28
  • no i don't want state. i want to transfer property Boolean from service 1 to service 2 and then in service 2 check if this property is true and if is true then i execute some code in this seocnd service... hope is better explanation – senzacionale Aug 16 '12 at 13:31
  • broadcast receivers will help you. – Yahor10 Aug 16 '12 at 13:34
  • do you know any example? Becouse i am already calling services from broadcast receiver. I know for intent.putExtra("me.SERVICE", somedata); but is it possible directly comuncate with services becouse i set property in service 1. – senzacionale Aug 16 '12 at 13:40
  • thank you. What about if i have one service and 2 broadcast receivers? – senzacionale Aug 16 '12 at 13:59
  • and i can comunicate between two broadcast receivers the same as between services? becouse i alo need this scenario. Thank you – senzacionale Aug 16 '12 at 15:58