2

我有一个Service,它使用HandlerThread每5秒运行一些代码。当我拨打stopSelf()时,服务似乎已停止,因为从设备设置 - >应用程序 - >运行它不显示在那里。 但是线程内部的代码每隔5秒就会持续运行,尽管我已经停止了该服务。
即使服务已停止,为什么HandlerThread仍然存在?当服务停止时,我如何停止HandlerThread服务器中的处理程序和线程继续运行,而服务似乎已停止

final Handler h = new Handler(); 
final int delay = 5000; // milliseconds 

h.postDelayed(new Runnable() { 
     public void run() { 
      // do something 
      final SharedPreferences sharedPref = getSharedPreferences(
        getString(R.string.prefs), Context.MODE_PRIVATE); 
      if (sharedPref != null) { 

       new Thread(new Runnable() { 
        @Override 
        public void run() { 
         // SOME CODE 
         Log.d("UPDATING"," something here"); 
         isUpdated = updateToServer(data); 
         if(isUpdated) { 
          sharedPref.edit().putBoolean("updated",true).commit(); 
          stopSelf(); 
         } 
        } 
       }).start(); 
      } 
      h.postDelayed(this, delay); 
     } 
    }, delay); 
+0

中的onDestroy可以调用handler.removeCallBacks(可运行) – Ramesh 2015-02-11 10:53:16

回答

2

发布Runnable之前,您应该验证isUpdated,如果isUpdated==true,不要再发布,代码如下:

final Handler h = new Handler(); 
final int delay = 5000; // milliseconds 

h.postDelayed(new Runnable() { 
     public void run() { 
      // do something 
      final SharedPreferences sharedPref = getSharedPreferences(
        getString(R.string.prefs), Context.MODE_PRIVATE); 
      if (sharedPref != null) { 

       new Thread(new Runnable() { 
        @Override 
        public void run() { 
         // SOME CODE 
         Log.d("UPDATING"," something here"); 
         isUpdated = updateToServer(data); 
         if(isUpdated) { 
          sharedPref.edit().putBoolean("updated",true).commit(); 
          stopSelf(); 
         } 
        } 
       }).start(); 
      } 
      if(!isUpdated){ 
       h.postDelayed(this, delay); 
      } 

     } 
    }, delay); 
+0

它很好用! – 2015-02-11 17:08:43

0

我想你必须在停止服务之前致电myHandler.getLooper().quit()

+0

你的意思我叫'h.getLooper()。退出()'只是调用'stopSelf前()'? – 2015-02-11 11:00:07

+0

@MohammedAli绝对。 – 2015-02-11 11:01:44

+0

这是导致崩溃!错误:'java.lang.RuntimeException:主线程不允许退出.' – 2015-02-11 15:44:23

1

正如其名,分别从服务的线程中运行,所以它不会只是停止通过停止服务。我没有真正测试这段代码,但它应该停止你的线程和处理程序,或者给你一个总体思路如何去做。

final Handler h = new Handler(); 
final int delay = 5000; // milliseconds 
private boolean isRunning = true; // a variable to signal stopping the thread 

h.postDelayed(new Runnable() { 
    public void run() { 
     // do something 
     final SharedPreferences sharedPref = getSharedPreferences(
       getString(R.string.prefs), Context.MODE_PRIVATE); 
     if (sharedPref != null) { 

      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        if (isRunning) { 
        // SOME CODE 
        Log.d("UPDATING"," something here"); 
        isUpdated = updateToServer(data); 
        if(isUpdated) { 
         sharedPref.edit().putBoolean("updated",true).commit(); 
         isRunning = false; // used to stop the thread from continuing the work 
         h.removeCallbacks(this); // stopping handler ("this" is the handler runnable you are stopping) 
         stopSelf(); 
        } 
        } 
       } 
      }).start(); 
     } 
     h.postDelayed(this, delay); 
    } 
}, delay); 

希望这有助于你...

+0

我还没有尝试过这种方式,但看起来这也可能起作用。 – 2015-02-12 17:47:54