首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在活动中停止Android服务

在活动中停止Android服务
EN

Stack Overflow用户
提问于 2012-01-12 13:21:38
回答 2查看 1.1K关注 0票数 0

我在停止StimulationService时遇到了困难,我不确定是否从我的活动中正确地调用了秒针方法。任何帮助都将不胜感激。

启动和停止服务的活动

代码语言:javascript
复制
     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;
    }
  }
}

}

服务

代码语言:javascript
复制
     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) {
                }
            }
        }
    }

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-13 02:41:34

你的活动没问题。问题是,该服务并没有扼杀IOIOThread。Thread.stop()是不推荐的,无论如何也不会做你想做的事情。您想要的是从服务的ioio_.disconnect() (通过线程类上的一个方法)调用onStop(),然后join()线程。以AbstracIOIOActivity为例。只要稍加修改,就可以将其转换为AbstractIOIOService,并使您能够将特定于应用程序的逻辑留在子类中。

票数 0
EN

Stack Overflow用户

发布于 2012-01-12 13:53:10

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

第二,永远不要停止后台线程。简单地将ioio_thread_数据成员设置为null并不会停止线程。那条线将永远运行下去。请不要这样做。如果没有其他的话,在您的AtomicBoolean循环中使用AtomicBoolean而不是硬连接的true,然后将该AtomicBoolean切换到onDestroy()中的false

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8835744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档