在 Activity 中停止 Android 服务

发布于 2024-12-26 06:48:34 字数 3344 浏览 0 评论 0原文

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

}

I'm having trouble STOPPING the StimulationService , I'm not sure if i'm calling the stopservice method correctly from my activity.
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) {
                }
            }
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回首观望 2025-01-02 06:48:34

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

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

First, as @Waqas notes, there is no onStop() method. There is an onDestroy() method, which will be called after stopService() is called.

Second, you are not stopping the background thread ever. Simply setting the ioio_thread_ data member to null does not stop the thread. 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().

や三分注定 2025-01-02 06:48:34

你的活动没问题。问题是该服务没有杀死 IOIOThread。
Thread.stop() 已被弃用,并且无论如何也不会执行您想要的操作。
您想要的是从服务的 onStop() 调用 ioio_.disconnect() (通过线程类上的方法),然后调用 join()< /code> 线程。
请参阅 AbstracIOIOActivity 作为示例。只需稍加修改,它就可以变成 AbstractIOIOService,并且使您能够将特定于应用程序的逻辑留在子类中。

Your activity is OK. The problem is that the service is not killing the IOIOThread.
Thread.stop() is deprecated and will not do what you want anyway.
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.
See AbstracIOIOActivity as an example. With minor modifications it can be turned into AbstractIOIOService, and will enable you to leave you application-specific logic in a subclass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文