我想每秒钟更新一次我的小部件。所以我使用服务和警报管理器,但它并不是每秒都更新一次。
这是我的日志列表:
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;
}
}
我怎么解决这个问题?我需要准确地每秒钟更新一次