onStart不是已经被onStartCommand取代了吗? @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.d(TAG, "onStartCommand() intent="+intent+", flags="+flags+", startId="+startId); return super.onStartCommand(intent, flags, startId); }
如果在run()里,有“写入SD卡的操作”,thr在Service的onCreate生成,说明服务只是生成一个线程。那么说,当第二次要写SD卡的话,调用StartService,假设第一次没有完成的话,并不会去执行第二次写操作了。public class BackgroundService extends Service { @Override public void onCreate() { super.onCreate(); Thread thr = new Thread(null, new ServiceWorker(), "BackgroundService"); thr.start(); } class ServiceWorker implements Runnable { public void run() { // do background processing here... // stop the service when done... // BackgroundService.this.stopSelf(); } }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d(TAG, "onStartCommand() intent="+intent+", flags="+flags+", startId="+startId);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
Thread thr = new Thread(null, new ServiceWorker(), "BackgroundService");
thr.start();
} class ServiceWorker implements Runnable
{
public void run() {
// do background processing here...
// stop the service when done...
// BackgroundService.this.stopSelf();
}
}
莫非一定要将onStartCommand(Intent intent那个保存下来,供run里用?