2014-06-21 56 views
2

我想爲android做一個鬧鐘應用程序我應該做的第一件事是不斷獲取系統的當前時間,所以我這樣做,但它只獲得當前第二個,沒有更多, ,有什麼幫助? 這裏是代碼:獲取當前時間不斷android

public class MainActivity extends Activity { 
    public Button time; 
    public TextView secondview; 
    public static int hours, mins, secs; 

    Handler main; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     time = (Button) findViewById(R.id.button1); 
     secondview = (TextView) findViewById(R.id.textView2); 

     new Thread(new Runnable() { 

      @Override 
      public void run() { 
       secondview.setText(String.valueOf(secs)); 
       try { 
        Thread.sleep(1); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     }).start(); 

     main = new Handler() { 

      @Override 
      public void handleMessage(Message msg) { 
       super.handleMessage(msg); 

       Calendar mycal = Calendar.getInstance(); 
       hours = mycal.get(Calendar.HOUR); 
       mins = mycal.get(Calendar.MINUTE); 
       secs = mycal.get(Calendar.SECOND); 

       secondview.setText(String.valueOf(secs)); 
      } 
     }; 
    } 
} 
+4

使用報警管理器 –

+5

使用這樣的線程是錯誤的,原因很多。它浪費CPU資源,速度慢,更新速度遠快於人類感知時間,並且無法工作(更改線程上的UI)。相反,你應該決定你想更新和使用計時器的頻率。每當定時器關閉時就獲取時間。 –

+0

@GabeSechan非常感謝,非常感謝:「D –

回答

2

HandlerThread是這種類型的問題不錯的選擇。請使用AlarmManager

您應該仔細選擇使用哪種類型的計時器。 (ELAPSED_REALTIME,RTC,....)
對於鬧鐘應用程序,RTC_WAKEUP是不錯的選擇。如何使用AlarmManager

報價:

// Set the alarm to start at approximately 2:00 p.m. 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 14); 

// With setInexactRepeating(), you have to use one of the AlarmManager interval 
// constants--in this case, AlarmManager.INTERVAL_DAY. 
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
     AlarmManager.INTERVAL_DAY, alarmIntent); 

有一些用例另一個例子。

0

Android提供兩種服務方式,一種是綁定到活動,當活動被刪除時,服務也是,另一種可以實現相同的功能,但它與活動沒有關係。在這裏我們需要使用第二個!

public class MainActivity extends Activity { 

private Button startService; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    startService = (Button) findViewById(R.id.startService); 
    startService.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(MainActivity.this, CountService.class); 
      startService(intent); 
     } 
    }); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    Intent intent = new Intent(MainActivity.this,CountService.class); 
    stopService(intent); 
}} 

public class CountService extends Service{ 

private int seconds=0; 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      while(seconds!=-1){ 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       Log.i("TIME","Time "+seconds); 
       //handle seconds use Calendar and AlarmManager 


       seconds++; 
      } 

     } 
    }).start(); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
}} 
0

我從某處找到了這個。它使用timertask。

public void updateTimeOnEachSecond() { 
    Timer timer = new Timer(); 
    timer.schedule(new TimerTask() { 

     @Override 
     public void run() { 
      c = Calendar.getInstance(); 
      Log.d("myapp", "time changed"); 
      hrs = c.get(Calendar.HOUR_OF_DAY); 
      min = c.get(Calendar.MINUTE); 
      sec = c.get(Calendar.SECOND); 

      runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
      txt_hrs.setText(String.valueOf(hrs)); 
      txt_mins.setText(String.valueOf(min)); 
      txt_sec.setText(String.valueOf(sec)); 
      } 
     }); 

     } 
    }, 0, 1000); 

}