停止活动中的Android服务

时间:2012-01-12 13:21:38

标签: java android android-service

我在停止StimulationService时遇到问题,我不确定我是否正在从我的活动中正确调用stopservice方法。 任何帮助将不胜感激。

启动和停止服务的活动

     public class Stimulation extends Activity implements OnClickListener {
  private static final String TAG = "StimulationActivity";
  Button buttonStart, buttonStop;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.someapp.Activities.R.layout.stimulation);

    buttonStart = (Button) findViewById(com.someapp.Activities.R.id.ButtonStart);
    buttonStop = (Button) findViewById(com.someapp.Activities.R.id.ButtonStop);

    buttonStart.setOnClickListener(this);
    buttonStop.setOnClickListener(this);
  }

  public void onClick(View src) {
    switch (src.getId()) {
    case com.someapp.Activities.R.id.ButtonStart:
      Log.d(TAG, "onClick: starting service");
      startService(new Intent(this, StimulationService.class));
      break;
    case com.someapp.Activities.R.id.ButtonStop:
      Log.d(TAG, "onClick: stopping service");
      stopService(new Intent(this, StimulationService.class));
      break;
    }
  }
}

}

服务

     public class StimulationService extends Service {
private static final String TAG = "StimulationService";
private IOIO ioio_;
private DigitalOutput led       


private volatile IOIOThread ioio_thread_;

public IBinder onBind(Intent intent) {
    return null;
}


public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();       
    Log.d(TAG, "onCreate");

}

public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    ioio_thread_.stop();

}

public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
    ioio_thread_ = new IOIOThread();
    ioio_thread_.start();

}

public void onStop(Intent intent, int stopid) {
    Log.d(TAG, "stop()");
    ioio_thread_ = null;
}


class IOIOThread extends Thread {
    private IOIO ioio_;
    private DigitalOutput led;

    /** Thread body. */
    public void run() {
        Thread thisThread = Thread.currentThread();
        super.run();

        while (ioio_thread_ == thisThread) {
            ioio_ = IOIOFactory.create();
            try{
                Log.d(TAG, "Wait for IOIO Connection");
                ioio_.waitForConnect();
                Log.d(TAG, "IOIOConnected");

                while (true) {
                    intializePins();
                    Log.d(TAG, "Pins Intialized");
                    while(true){
                        led.write(false);
                        sleep(2000);
                        led.write(true);
                        sleep(2000);
                    }
                }

            }


            catch (ConnectionLostException e) {
            } catch (Exception e) {
                Log.e("Hello", "Unexpected exception caught", e);
                ioio_.disconnect();
                break;
            } finally {
                try {
                    ioio_.waitForDisconnect();
                } catch (InterruptedException e) {
                }
            }
        }
    }

}

2 个答案:

答案 0 :(得分:1)

首先,正如@Waqas所说,没有onStop()方法。有onDestroy()方法,将在调用stopService()后调用。

其次,你没有停止后台线程。只需将ioio_thread_数据成员设置为null即可停止该线程。该线程将永远运行。请不要这样做。如果不出意外,请在AtomicBoolean循环中使用true代替硬连线while(),并在AtomicBoolean中将false翻转为onDestroy()

答案 1 :(得分:0)

您的活动没问题。问题是服务没有杀死IOIOThread。 Thread.stop()已弃用,无论如何都不会执行您想要的操作。 你想要的是从服务的ioio_.disconnect()(通过线程类的方法)调用onStop(),然后join()线程。 请参阅AbstracIOIOActivity作为示例。通过微小的修改,它可以转换为AbstractIOIOService,并使您能够将特定于应用程序的逻辑留在子类中。