0

I am new to android development, i am working on a project in which i have to create two service namely service1 and service2.

My service1 will run always while it starts a new service called service2 which will run for some time perform some work and stop.

service2 has to read/access and change the value of some data members of the service1 and get destroy automatically every time after completing the execution.

Till now i came to know i want to perform some inter service communication.

I dont know how to do this... Kindly help me to solve this problem.

Service1 Code Snippet

class Service1 extends Service{
        private int counter = 0;
//some code
@Override
        public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate(); 
Intent i= new Intent(this,StratCounterService.class);
        startService(i);
}
//some code
    public int getCounter() {
            return counter;
        }

This is the code which i am using in the class Service1.java

Service2 Code Snippet

public class Service2 extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub

        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(20000);
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    if(getCounter()>=someInteger) doSomthing();
                    else {
                        mInitialized=false;
                        //And stop this service here!
                        stopSelf();
                    }
                }
            }
        };
        for(int i=0;i<3;i++) timer.start();
    }

    @Override
    public void onRebind(Intent intent) {
        // TODO Auto-generated method stub
        super.onRebind(intent);
    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        return super.onStartCommand(intent, flags, startId);
    }


}

Thanx in advance..

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Suraj
  • 1
  • 4
  • 1
    You can save it into Bundled extras in Intent that fire service. Try to look on http://stackoverflow.com/a/21565858/1370062 – RMachnik Apr 15 '14 at 10:47
  • I didnt get how to do this can you give examples to do this? – Suraj Apr 15 '14 at 10:50
  • That examples are included in answer that I gave you. There is another one http://www.codelearn.org/android-tutorial/android-intent – RMachnik Apr 15 '14 at 10:53

1 Answers1

2

you can use shared preference to store data and can get it from your another service class

SharedPreferences

Dhwanik Gandhi
  • 685
  • 6
  • 12