2013-01-05 63 views
0

我正在嘗試使用timertask更新我的數字時鐘。我創建了一個名爲updateClock()的函數,它將小時和分鐘設置爲當前時間,但我無法定期運行它。從我在其他答案中看到的其中一個最好的選擇是使用timertask,但是我還沒有能夠在Android活動中發現任何在線工作的例子。使用timertask每X秒更新一次時鐘?

這是我到目前爲止已經寫的:

public class MainActivity extends Activity { 
    TextView hours; 
    TextView minutes; 
    Calendar c; 
    int cur_hours; 
    int cur_minutes; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.clock_home); 
     hours = (TextView) findViewById(R.id.hours); 
     minutes = (TextView) findViewById(R.id.minutes); 
     updateClock(); 
     } 

    public void updateClock() { 
     c = Calendar.getInstance(); 
     hours.setText("" + c.get(Calendar.HOUR)); 
     minutes.setText("" + c.get(Calendar.MINUTE)); 
     } 

    public static void init() throws Exception { 
     Timer timer = new Timer(); 
     timer.scheduleAtFixedRate(new TimerTask() { 
      public void run() { 
        updateClock(); // ERROR 
       } 
      }, 0, 1 * 5000); 
     } 
    } 

我怎樣才能使它發揮作用?

+0

Android已經在['Chronometer'(http://developer.android.com/reference/android/widget/Chronometer.html)如果你有興趣。 – Sam

回答

0

如果您只是需要每分鐘更新一次,您還可以收聽ACTION_TIME_TICK廣播事件。

private boolean timeReceiverAttached; 
private final BroadcastReceiver timeReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     updateClock(); 
    } 
}; 
private Handler handler = new Handler(); 

@Override 
protected void onResume() { 
    super.onResume(); 
    updateClock(); 
    if (!timeReceiverAttached) { 
     timeReceiverAttached = true; 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(Intent.ACTION_TIME_TICK); 
     filter.addAction(Intent.ACTION_TIME_CHANGED); 
     filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 
     registerReceiver(timeReceiver, filter, null, handler); 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (timeReceiverAttached) { 
     unregisterReceiver(timeReceiver); 
     timeReceiverAttached = false; 
    } 
} 
1

使用runOnUiThread從計時器線程更新UI

timer.scheduleAtFixedRate(new TimerTask() { 
    public void run() { 
     MainActivity.this.runOnUiThread (new Runnable() { 
     public void run() { 
      updateClock(); // call UI update method here 
     } 
    })); 
    } 
}, 0, 1 * 5000); 
} 
0

OR,定期發佈可運行於UI線程的處理程序。此外,暫停和恢復任務以節省電量。

public class MyActivity extends Activity { 
    private final Handler mHandler = new Handler(); 
    private final Timer mTimer = new Timer(); 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     mTimer.scheduleAtFixedRate(new TimerTask() { 
      @Override 
      public void run() { 
       mHandler.post(new Runnable() { 
        @Override 
        public void run() { 
         //---update UI--- 
        } 
       }); 
      } 
     },0,5000); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     mTimer.cancel(); 
    } 
}