java - 在 Activity 中停止 Android 服务

标签 java android android-service

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

启动和停止服务的 Activity

     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,然后翻转该 AtomicBoolean > 在 onDestroy() 中设置为 false

关于java - 在 Activity 中停止 Android 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835744/

相关文章:

android - 为什么在 Android 中调用 stopService 后服务中的线程仍未停止?

java - setOnKeyListener onKey 不适用于 Eclipse/ADT Android 虚拟设备 - Nexus One

java - SurfaceView运行方法代码

java - 应用程序被终止时服务正在重新启动

java - 在服务中同步执行长时间运行的方法

java - 由于无法读取源文件,该项目未构建

java - WHERE + AND 查询不起作用 SQLite

java - @FunctionalInterface 在分阶段构建器中的好处

java - Admob 插页式广告显示在游戏中间

java - Collections.sort 编译错误 - 不兼容的类型