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;
}
}
我怎樣才能解決這個問題?我需要更新都完全每秒鐘
感謝您的回覆?但它沒有正常工作有我的代碼: – vic 2010-09-16 15:13:15
最終處理程序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