views:

42

answers:

1

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

}

How can I solve this problem? I need update all exactly every seconds

A: 

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.

Damian Kołakowski
Thanks for your response? but it doent work correctly Thereis my code:
vic
final Handler mHandler=new Handler(Looper.getMainLooper()); Thread t=new Thread(new Runnable() { public void run() { Log.i("TEST","FIRST_TIME"+FIRST_TIME_RUNNING+"when="+when+"time="+getTimeString()); curTime=System.currentTimeMillis(); writer.writeNext(strmas); if(!FIRST_TIME_RUNNING){ when=curTime-prevTime; } FIRST_TIME_RUNNING=false; prevTime=curTime; mHandler.postDelayed(this, when); } }); t.start();
vic