我有一个IntentService
类,我希望用一个警报让服务每X小时执行一次任务。
我检索警报并设置它,但服务每2秒执行一次任务,而不是每X小时执行一次;它永远不会停止:BroadcastReceiver
始终触发Intent
。
这是代码:
@Override
protected void onHandleIntent(Intent intent) {
int alarmType = AlarmManager.RTC_WAKEUP;
username = intent.getStringExtra("username");
//android.os.Debug.waitForDebugger();
alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
context = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
serviceToStart = intent.getStringExtra("serviceToStart");
if (serviceToStart.equals("calls")) {
String CALLS_ALARM_ACTION = "ACTION_CALLS_LOGGING";
Intent callsIntentToFire = new Intent(CALLS_ALARM_ACTION);
callsIntentToFire.putExtra("serviceToStart", serviceToStart);
callsAlarmIntent = PendingIntent.getBroadcast(context, 0, callsIntentToFire, 0);
callsUpdatefrequency = toLong(Integer.parseInt(prefs.getString(Preferences.CALLS_FREQUENCY_PREF, "0")));
long callsTimeToUpdate = SystemClock.elapsedRealtime() + callsUpdatefrequency;
alarms.setRepeating(alarmType, callsTimeToUpdate, callsUpdatefrequency, callsAlarmIntent);
dumpCallsLog();
最后一个方法(dumpCallsLog())是更新远程数据库的方法。 我有什么遗漏或代码中的错误吗?
这是我的BroadcastReceiver
代码:
public class LoggingReceiver extends BroadcastReceiver {
public static final String ACTION_CALLS_LOGGING = "ACTION_CALLS_LOGGING";
@Override
public void onReceive(Context context, Intent intent) {
Intent startIntent = new Intent(context, LoggingService.class);
String action = intent.getStringExtra("serviceToStart");
startIntent.putExtra("serviceToStart", action);
context.startService(startIntent);
}
}
答案 0 :(得分:1)
完成!我的坏:
String CALLS_ALARM_ACTION = "ACTION_CALLS_LOGGING";
Intent callsIntentToFire = new Intent(CALLS_ALARM_ACTION);
callsIntentToFire.putExtra("serviceToStart", "dumpCalls");
callsIntentToFire.putExtra("username", username);
callsAlarmIntent = PendingIntent.getBroadcast(context, 0, callsIntentToFire, 0);
callsTriggerAlarmTime = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long callsAlarmInterval = 30000L;
// toLong(Integer.parseInt(prefs.getString(Preferences.CALLS_FREQUENCY_PREF,
// "86400000")));
alarms.setRepeating(alarmType, callsTriggerAlarmTime,
callsAlarmInterval, callsAlarmIntent);