繁体   English   中英

在活动中停止Android服务

[英]Stopping a Android service in an Activity

我在停止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) {
                }
            }
        }
    }

}

首先,正如@Waqas指出的那样,没有onStop()方法。 有一个onDestroy()方法,将在调用stopService()之后调用该方法。

其次,您永远不会停止后台线程。 只需将ioio_thread_数据成员设置为null不会停止线程。 该线程将永远运行。 请不要这样做。 如果没有其他问题,请while()循环中使用AtomicBoolean而不是true ,然后在onDestroy() AtomicBoolean翻转为false

您的活动正常。 问题在于该服务没有终止IOIOThread。 Thread.stop()已被弃用,无论如何也不会做您想要的事情。 您想要的是从服务的onStop()调用ioio_.disconnect() (通过线程类上的方法),然后将线程join() 请参见AbstracIOIOActivity作为示例。 通过较小的修改,它可以变成AbstractIOIOService ,并使您可以将应用程序特定的逻辑留在子类中。

暂无
暂无

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

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