2010-09-15 82 views
0

我想每秒更新一次我的小部件。所以我使用服務和報警管理器,但不會每秒更新一次。如何每秒更新數據? AlarmManager

這是我的日誌列表:

09-15 11:16:08.876: INFO/REPEAT(2850): time=11:16:08am 
    09-15 11:16:09.970: INFO/REPEAT(2850): time=11:16:09am 
    **09-15 11:16:11.025: INFO/REPEAT(2850): time=11:16:11am 
    09-15 11:16:11.978: INFO/REPEAT(2850): time=11:16:11am** 
    09-15 11:16:13.118: INFO/REPEAT(2850): time=11:16:12am 
    09-15 11:16:14.048: INFO/REPEAT(2850): time=11:16:13am 
    09-15 11:16:15.900: INFO/REPEAT(2850): time=11:16:15am 
    09-15 11:16:16.533: INFO/REPEAT(2850): time=11:16:16am 
    09-15 11:16:17.595: INFO/REPEAT(2850): time=11:16:17am 
    09-15 11:16:17.845: INFO/REPEAT(2850): time=11:16:17am 
    09-15 11:16:18.892: INFO/REPEAT(2850): time=11:16:18am 
    09-15 11:16:20.212: INFO/REPEAT(2850): time=11:16:19am 
    09-15 11:16:21.025: INFO/REPEAT(2850): time=11:16:20am 
    09-15 11:16:21.861: INFO/REPEAT(2850): time=11:16:21am 

這是我的代碼:

public static final int INTERVAL = 1000; // 1 sec 
public static final int FIRST_RUN = 0; // 0 seconds 

    private void startService() { 

    Intent intent = new Intent(this, RepeatingAlarmService.class); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0); 

    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(
      AlarmManager.ELAPSED_REALTIME_WAKEUP, 
      SystemClock.elapsedRealtime() + FIRST_RUN, 
      INTERVAL, 
      pendingIntent); 

    Toast.makeText(this, "Service Started.", Toast.LENGTH_LONG).show(); 
    Log.v(this.getClass().getName(), "AlarmManger started at " + new java.sql.Timestamp(System.currentTimeMillis()).toString()); 
} 

RepeatingAlarmService.class:

public class RepeatingAlarmService extends BroadcastReceiver { 
public static final String CUSTOM_INTENT = "time to write"; 

@Override 
public void onReceive(Context context, Intent intent) { 

    Log.i("REPEAT","time="+getTimeString()); 
    Intent i = new Intent(); 
    i.setAction(CUSTOM_INTENT); 
    context.sendBroadcast(i); 


} 



public static String getTimeString() { 
    String result = null; 
    Calendar cal = Calendar.getInstance(); 
    if(cal.get(Calendar.AM_PM) == Calendar.AM) 
     result = new String(String.format("%02d:%02d:%02dam", cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE),cal.get(Calendar.SECOND))); 
    else 
     result = new String(String.format("%02d:%02d:%02dpm", cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE),cal.get(Calendar.SECOND))); 
    return result; 
} 

}

我怎樣才能解決這個問題?我需要更新都完全每秒鐘

回答

2

從Android電子手冊:

注:報警管理器是要在其中有你 應用程序代碼運行在特定 時間的情況下,預期 即使你應用程序不是 當前正在運行。對於正常的時機 操作(滴答,超時等),它 更容易和更有效 使用處理程序。

您可以使用AlarmManager而不是從服務器等獲取數據。僅適用於每5/15分鐘應執行的操作。

動畫使用Handler。

我也有你的建議。不要根據固定的時間間隔構建動畫。它們只能在RTOS機器上順利工作>:]您應該計算先前和當前更新之間的時間間隔。使用時間跨度模擬下一幀。

+0

感謝您的回覆?但它沒有正常工作有我的代碼: – vic 2010-09-16 15:13:15

+0

最終處理程序mHandler =新處理程序(Looper.getMainLooper()); \t \t線程t =新主題(新的Runnable(){ \t \t \t \t \t \t公共無效的run(){ \t \t \t \t Log.i( 「TEST」, 「FIRST_TIME」 + FIRST_TIME_RUNNING + 「時=」當+ + 「時間=」 + getTimeString()); \t \t \t \t CURTIME = System.currentTimeMillis的(); \t \t \t \t writer.writeNext(strmas); \t \t \t \t if(!FIRST_TIME_RUNNING){ \t \t \t \t \t when = curTime-prevTime; \t \t \t \t} \t \t \t \t FIRST_TIME_RUNNING = FALSE; \t \t \t \t prevTime = curTime; \t \t \t \t mHandler.postDelayed(this,when); \t \t \t} \t \t}); \t \t t.start(); – vic 2010-09-16 15:13:56