gpt4 book ai didi

java - 在 Activity 中停止 Android 服务

转载 作者:行者123 更新时间:2023-12-01 15:35:58 26 4
gpt4 key购买 nike

我在停止 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com