2012-02-07 31 views
2

我想在countdowntimer後臺應用程序,即如果我開始 定時器和出來的那個應用程序,並去到另一個應用程序,然後 回來同countdowntimer應用。我想讓那個定時器運行到 直到我停下來。我知道涉及的方法,但我不確定 關於它使用的線程概念。如何使用線程概念在countdowntimer中運行後臺應用程序?

//MyActivity.class 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class MyappActivity extends Activity 
    { 
    private static final String Tag = "Background_Timer"; 
    private Button start; 
    private Button stop; 
    private TextView tv; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     start = (Button) findViewById(R.id.button); 
     stop = (Button) findViewById(R.id.button1); 

     tv = (TextView) findViewById(R.id.text1); 

     this.runOnUiThread(new Runnable() 
     { 
      public void run() 
      { 
       tv.setText(MyAppService.seconds + " Seconds Left"); 
      } 
     }); 

    } 

    public void onClick(View src) 
    { 
     switch (src.getId()) 
     { 
     case R.id.button: 
      Log.e(Tag, "onClick: starting service"); 
      startService(new Intent(this, MyAppService.class)); 
      break; 

     case R.id.button1: 
      Log.e(Tag, "onClick: stopping service"); 
      stopService(new Intent(this, MyAppService.class)); 
      break; 
     } 
    } 
     } 

     //MyService.class 

     import android.app.Service; 
     import android.content.Intent; 
     import android.os.CountDownTimer; 
     import android.os.IBinder; 
     import android.util.Log; 
     import android.widget.TextView; 

     public class MyAppService extends Service 
     { 

    private static final String TAG = "My Service"; 
    private static boolean state; 
    private static TextView TextTimer; 
    public static String seconds; 

    MyThread mt = new MyThread(); 

    @Override 
    public void onCreate() 
    { 

     CountDownTimer Myapp = new CountDownTimer(50000, 1000) 
     { 
      public void onTick(long millisUntilFinished) 
      { 
       TextTimer.setText("Seconds left: " + (millisUntilFinished) 
         /1000); 
      } 

      public void onFinish() 
      { 
       TextTimer.setText("Finished!"); 
      } 
     }; 
    } 

    public void onStart(Intent intent, int startid) 
    { 
     Log.d(TAG, "on-Start"); 
     mt.start(); 
    } 

    public void onDestroy() 
    { 
     Log.d(TAG, "on-Stop"); 
     mt.stop(); 
    } 

    public static class MyThread extends Thread 
    { 

     public void run() 
     { 
      try 
      { 
       while (state = true) 
       { 
        MyThread.sleep(500); 
       } 
      } 
      catch (InterruptedException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }; 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) 
    { 
     // TODO Auto-generated method stub 
     return null; 
    } 
     } 

回答

1

您可以使用TimerTask類用於此目的。它與線程相同,但允許您基於時間間隔執行任務。您也可以在其運行方法中執行可重複的任務。

請檢查簡單的例子here

0

第一關:你不應該運行在應用程序UI線程CountDownTimer,因爲這可能導致ANR數(應用程序無響應)的問題。

您可以使用SheduledThreadPoolExecutor與您的自定義Runnable或使用Bound Service。回撥實際活動(如果它的運行,你可以使用不同的方法,如通過Observer Pattern認購或發送,周圍用LocalBroadcastManager意圖,然後更新它自己的線程的用戶界面。

您也可以使用TimerTask的狂妄具有指出,但要確保更新視圖當了Runnable調用runOnUiThread。

1

在你的活動接收的蜱,並完成

的消息

在服務的靜態處理程序具有開始倒計時這樣一個主題的CountDown將在您的Thread中工作,而不是在主線程中。