2012-01-12 118 views
0

我遇到問題停止StimulationService,我不確定是否從我的活動中正確調用stopservice方法。 任何幫助將不勝感激。停止活動中的Android服務

活動的啓動和停止服務

 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) { 
       } 
      } 
     } 
    } 

}

+0

你的代碼對我來說似乎是正確的。你怎麼說服務沒有停止? – waqaslam 2012-01-12 13:26:49

+0

當我單擊以停止活動屏幕上的服務時,IOIO板上的LED繼續閃爍。 – 2012-01-12 13:36:34

+0

我看到現在發生了什麼..它去的onDestroy()函數,而不是stop()函數......任何想法? – 2012-01-12 13:39:27

回答

0

你的活動還行。問題是該服務沒有殺死IOIOThread。 Thread.stop()已棄用,不會做你想做的事情。 你想要的是從服務的onStop()(通過你的線程類上的方法)調用ioio_.disconnect(),然後join()線程。 以AbstracIOIOActivity爲例。只需稍作修改,它就可以變成AbstractIOIOService,並且使您能夠將應用程序特定的邏輯留在子類中。

+0

Hey Ytai,我假設你是Ytai Ben-Tsvi,是否可以使用通過USB連接到它的手機爲IOIO設備供電? – 2012-01-13 16:43:12

+0

這是我的確。答案是不。 IOIO是一個USB主機。 – Ytai 2012-01-14 02:03:08

1

首先,@Waqas筆記,沒有onStop()方法。有一個onDestroy()方法,將在調用stopService()後調用。

其次,你並沒有停止後臺線程。簡單地將ioio_thread_數據成員設置爲null不會停止該線程。該線程將繼續運行。請不要這樣做。如果沒有其他情況,請在您的while()循環中使用AtomicBoolean而不是硬連線true,並將AtomicBoolean翻轉爲中的false

+0

感謝您的建議,你將如何實現AtomicBoolean到這個代碼? – 2012-01-12 14:19:31

+1

@DavidFlanagan:它是服務的數據成員。在分割線之前將其設置爲「true」。在你的'while()'循環中檢查它(替換硬編碼的'true')。在'onDestroy()'中將它設置爲'false'。現在,我只注意到你的'ioio_thread_ == thisThread'檢查,這可能會解決同樣的問題,儘管你可能需要聲明'ioio_thread_'爲'volatile'。 – CommonsWare 2012-01-12 17:10:05