0

ウィジェットを毎秒更新したい。そのため、Service and Alarm Manager を使用していますが、正確に毎秒更新されるわけではありません。

これは私のログリストです:

  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;
}

}

どうすればこの問題を解決できますか? 正確に毎秒すべて更新する必要があります

4

1 に答える 1

2

アンドロイドのマニュアルから:

注: Alarm Manager は、アプリケーションが現在実行されていない場合でも、特定の時間にアプリケーション コードを実行する場合を対象としています。通常のタイミング操作 (ティック、タイムアウトなど) では、Handler を使用する方が簡単で効率的です。

サーバーなどからデータを取得するのではなく、AlarmManager を使用できます。5/15 分ごとに実行する必要があるアクションにのみ使用できます。

アニメーションには Handler を使用します。

私からもアドバイスがあります。固定間隔に基づいてアニメーションを作成しないでください。それらは RTOS マシンでのみスムーズに動作します >:] 以前の更新と現在の更新の間のタイムスパンを計算する必要があります。タイムスパンを使用して、次のフレームをシミュレートします。

于 2010-09-15T09:06:58.303 に答える