2014-11-20 110 views
0

我有一個Android的服務如下:在Android上的線程中運行無限循環時,應用程序崩潰。

public class TestService extends Service { 

    . 
    . 
    . // Variables 
    . 
    private Thread ChangeColors; 
    private final int[] colors = {Color.BLACK, Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN}; 

    @Override 
    public void onStartCommand(Intent intent, int flags, int startID) { 

     LinearLayout layout = TestActivity.getLayout(); // Returns the layout from the activity class 
     changeColors = new Thread() { 

      int index = 0; 


      @Override 
      public void run() { 

        while(!isInterrupted()) { 

         try { 

          layout.setBackgroundColor(colors[index]); // Set the background to the Color at the index'th position in the array. 
          index ++; // Increment the index count. This will throw ArrayIndexOutOfBoundsException once the index > colors.length 
         } catch(ArrayIndexOutOfBoundsException exception) { 

          index = 0; // Then simply set the value of index back to 0 and continue looping. 
         } 
        } 
      } 
     }; 

     changeColors.start(); 
     return START_STICKY; 
    } 

    . 
    . 
    Other methods 
    . 
    . 

    @Override 
    public void onDestroy() { 

     super.onDestroy(); 
     changeColors.interrupt(); 
     Toast.makeText(this, "Stopped!", Toast.LENGTH_SHORT).show(); 
    } 
} 

該服務只是獲取android.widget.LinearLayout實例從Activity上的按鈕,點擊不斷變化的LinearLayout的背景色。

這是活動:

public class TestActivity extends Activity { 


    private static LinearLayout layout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     layout = new LinearLayout(this); 
     layout.setOrientation(LinearLayout.VERTICAL); 
     layout.setLayoutParams(new LinearLayout.LayoutParams(

      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.FILL_PARENT 
     )); 

     Button butt = new Button(this); 
     butt.setText("Start thread!"); 
     button.setLayoutParams(new LinearLayout.LayoutParams(

      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT 
     )); 


     layout.addView(butt); 
     setContentView(layout); 


     butt.setOnClickListener(

      new View.OnClickListener() { 

       @Override 
       public void onClick(View view) { 

        butt.setBackgroundColor(Color.BLACK); 
        startService(getContextBase(), TestService.class); 
       } 
      } 
     ); 

     // I have another button coded here which on clicking calls the stopService() method. 
    } 



    public static LinearLayout getLayout() { 

     return layout; 
    } 
} 

程序似乎是相當簡單的。該應用在我的LG-L90手機上啓動得很好。但只要我點擊butt按鈕,butt的顏色就會變爲黑色,並且應用程序會立即崩潰,而不會在Service中運行循環。

我哪裏錯了?請幫忙。我真的想看看線程如何幫助我們做這些事情,並不斷改變可能有助於我在遊戲開發中的GUI。

在此先感謝。

+0

我認爲你必須在UI線程中進行UI操作。將這部分代碼放在'runOnUiThread'中。 – 2014-11-20 21:00:03

+0

你能爲我寫代碼嗎? M新的android。但是有了Java的經驗。儘管如此,從未編寫過多線程應用程序。 – 2014-11-20 21:02:04

回答

0

您應該使用runOnUiThread進行UI操作。

public class MyService extends Service { 

    Handler handler; 

    public MyService() { 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     handler = new Handler(); 
    } 

    private void runOnUiThread(Runnable runnable) { 
     handler.post(runnable); 
    } 

    ... 
} 

,並確保您導入android.os.Handler,現在稱之爲runOnUiThread方法。

+0

runOnUiThread()是'android.app.Activity'中的一個方法。有沒有什麼辦法可以從'android.app.Service'調用它? – 2014-11-21 06:58:17

+0

看到我更新的答案。你必須使用[Handler](http://developer.android.com/reference/android/os/Handler.html) – 2014-11-21 11:30:16

+0

號。它不工作 – 2014-11-25 09:47:48

相關問題