0
这些方法在扩展Service的类中实现。每次按下按钮时都会启动一项新服务。多线程生命周期中的多服务
如果按下按钮一次,LogCat会输出我期望的内容,其中包括“Service onDestroy”的最终输出。但是,当按钮被按下两次时,第一次服务结束之前的第二次“Service onDestroy”。第一项服务完成后仅显示一次,第二项服务打印出“服务正在运行”日志的其余部分,但不打印“Service onDestroy”。
有谁能告诉我为什么?谢谢!
public int onStartCommand(Intent intent,int flags, int startId) {
Log.i(TAG,"Service onStartCommand " + startId);
final int currentId = startId;
Runnable r = new Runnable() {
public void run() {
for (int i = 0; i < 3; i++) {
long endTime = System.currentTimeMillis() + 10*1000;
while (System.currentTimeMillis() < endTime) {
synchronized(this) {
try {
wait(endTime - System.currentTimeMillis());
}catch (Exception e) {
}
}
}
Log.i(TAG,"Service running");
}
stopSelf();
}
};
Thread t = new Thread(r);
t.start();
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG,"Service onBind");
return null;
}
@Override
public void onDestroy() {
Log.i(TAG,"Service onDestroy");
}