简体   繁体   English

在活动中停止Android服务

[英]Stopping a Android service in an Activity

I'm having trouble STOPPING the StimulationService , I'm not sure if i'm calling the stopservice method correctly from my activity. 我在停止StimulationService时遇到问题,不确定我是否从活动中正确调用了stopservice方法。 Any help will be much appreciated. 任何帮助都感激不尽。

Activity to start and stop Service 启动和停止服务的活动

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

} }

Service 服务

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

} }

First, as @Waqas notes, there is no onStop() method. 首先,正如@Waqas指出的那样,没有onStop()方法。 There is an onDestroy() method, which will be called after stopService() is called. 有一个onDestroy()方法,将在调用stopService()之后调用该方法。

Second, you are not stopping the background thread ever. 其次,您永远不会停止后台线程。 Simply setting the ioio_thread_ data member to null does not stop the thread. 只需将ioio_thread_数据成员设置为null不会停止线程。 That thread will keep running forever. 该线程将永远运行。 Please do not do this. 请不要这样做。 If nothing else, use an AtomicBoolean instead of a hardwired true in your while() loop, and flip that AtomicBoolean to false in onDestroy() . 如果没有其他问题,请while()循环中使用AtomicBoolean而不是true ,然后在onDestroy() AtomicBoolean翻转为false

Your activity is OK. 您的活动正常。 The problem is that the service is not killing the IOIOThread. 问题在于该服务没有终止IOIOThread。 Thread.stop() is deprecated and will not do what you want anyway. Thread.stop()已被弃用,无论如何也不会做您想要的事情。 What you want is to call ioio_.disconnect() from the service's onStop() (through a method on your thread class), and then join() the thread. 您想要的是从服务的onStop()调用ioio_.disconnect() (通过线程类上的方法),然后将线程join() See AbstracIOIOActivity as an example. 请参见AbstracIOIOActivity作为示例。 With minor modifications it can be turned into AbstractIOIOService , and will enable you to leave you application-specific logic in a subclass. 通过较小的修改,它可以变成AbstractIOIOService ,并使您可以将应用程序特定的逻辑留在子类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM