2010-09-15 57 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