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