不要在OnStartCommand方法中执行,在OnStart方法中执行数据库操作

解决方案 »

  1.   

    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);
    }
      

  2.   

    如果在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();
        }
    }
      

  3.   

    不好意思,是要在OnCreate方法中执行
      

  4.   

    另外,想在class ServiceWorker implements Runnable 里获得Intent里数据,该如何?
    莫非一定要将onStartCommand(Intent intent那个保存下来,供run里用?