我是Android开发的新手,我正在开发一个项目,我必须创建两个服务,即 service1 和 service2
我的 service1 会在启动名为 service2 的新服务时运行,该服务会运行一段时间做一些工作并停下来。
service2 必须读取/访问并更改某些数据成员的值 service1 并在每次完成执行后自动销毁。
直到现在我才知道我想进行一些服务间沟通。
我不知道该怎么做...请帮我解决这个问题。
Service1代码段
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;
}
这是我在Service1.java类中使用的代码
Service2代码段
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);
}
}
提前完成..