2012-02-07 16 views
2

私はカウントダウンタイマでバックグラウンドアプリケーションを作っています。つまり、タイマが で始まり、そのアプリケーションから出てきて、別のアプリケーションに行くと、同じ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クラスを使用できます。スレッドと同じですが、時間間隔に基づいてタスクを実行できます。 runメソッドで繰り返し可能なタスクを実行することもできます。

簡単な例hereを確認してください。

0

第1オフ:アプリケーションUIスレッドでCountDownTimerを実行しないでください。これは、ANR(アプリケーションが応答しない)問題を引き起こす可能性があるためです。

SheduledThreadPoolExecutorを独自のRunnableで使用するか、Bound Serviceを使用します。それが実行している場合(実際の活動をコールバックするには、Observer Patternを経由して加入するように異なる方法を使用するか、LocalBroadcastManagerを使用してインテントを周りに送信し、それ自身のスレッドでUIを更新することができます。

ルシファーが持っているあなたも、TimerTaskをを使用することができますあなたのようにするにはカウントダウンを開始するスレッドを持って指摘したが、ビューを更新するときのRunnableがrunOnUiThreadを呼び出すことを確認してください。

1

はサービスにチェックし、[完了

のあなたのメッセージを受け取り、あなたの活動の静的ハンドラを持っていますCountDownはメインスレッドではなくあなたのスレッドで動作します。

関連する問題