Stopping an Android Service from within a thread

混江龙づ霸主 提交于 2019-12-05 10:16:00

I think what you want here might be to use a Handler. Eseentially, create a handler for your Sevrice (make sure it's final so you can use across all threads). Then in your other thread, call:

myServiceHandler.post(new Runnable(){
  public void run(){
    stopSelf();
  }
}

How about storing the Intent of the service and stopping it directly? In the service:

private Intent thisIntent;

public int onStartCommand (Intent intent, int flags, int startId) {

    thisIntent = intent;

In the thread inside the service:

stopService(thisIntent);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!