2012-02-07 111 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中工作,而不是在主线程中。