I want update my widget every seconds. So I use Service and Alarm Manager, but it doesn't update exactly every second.
This my log list:
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
This is my code:
public static final i开发者_如何学运维nt 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;
}
}
How can I solve this problem? I need update all exactly every seconds
From android manual:
Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.
You can use AlarmManager rather for getting data from server, etc. Only for actions which should be done every 5/15 minutes.
For animations use Handler.
I have also advice for you. Don't build animation based on fixed intervals. They will work smoothly only on RTOS machine >:] You should calculate timespan between previous and current updates. Use the timespan do simulate next frame.
精彩评论