我正在使用离线任务,我必须每5分钟在backgourd中调用Webservice
。所以我正在使用AlarmManager
。
第一次AlarmManager
点火开启时间(开始5分钟后),但是比起它在几秒钟内开火,我不知道为什么?
我只想每5分钟点火一次......
请帮助..
这是启动计时器的代码..
public void startOfflineDateAlarm() {
//* For Start Offline Sync Timer*//
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, Constant.offlineDataAlarmTime,
PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Log.e(TAG, "ALARM START");
saveOfflineTables(getApplicationContext());
}
这是BroadcastReceiver
,我正在为调用Webservices
执行任务。
我还在AlarmManager
中添加了启动BroadcastReceiver
的代码,以便在接下来的5分钟内再次启动AlarmManager。(如果错误的话请纠正我。)
public class AlarmReceiver extends BroadcastReceiver {
String TAG = "AlarmReceiver";
public static PendingIntent pendingIntent;
public static AlarmManager alarmManager;
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "ALARM CLICKED");
//* For Start Offline Sync Timer*//
Intent intentAlarm = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,Constant.offlineDataAlarmTime, pendingIntent);
saveOfflineTables(context);
syncOfflineDataWithServer(context);
}
private void saveOfflineTables(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("SFAData", MODE_PRIVATE);
String screenCheck = sharedPreferences.getString("CheckScreen", "");
String userId = sharedPreferences.getString("id", Constant.DEFAULT);
String storeCode = sharedPreferences.getString("store_code", DEFAULT);
String storeId = sharedPreferences.getString("store_ID", DEFAULT);
Log.e(TAG, "saveOfflineTables");
OfflineDataSave.saveItemList(context, userId);
OfflineDataSave.savePromotionList(context, storeId);
OfflineDataSave.saveSubCategoryList(context,userId);
OfflineDataSave.saveSubSubCategoryList(context,userId);
OfflineDataSave.saveDistributorList(context, userId);
OfflineDataSave.saveStoreList(context, userId);
OfflineDataSave.saveBAList(context, userId);
OfflineDataSave.saveCustomerList(context, userId);
OfflineDataSave.saveDailyTask(context, userId);
OfflineDataSave.saveTargetVsAchievement(context, userId);
}
private void syncOfflineDataWithServer(Context context){
DataSyncWithServer.syncAttendance(context);
DataSyncWithServer.syncCounterHygiene(context);
DataSyncWithServer.syncCompetition(context);
DataSyncWithServer.syncCustomerRegistration(context);
DataSyncWithServer.syncMakeOver(context);
DataSyncWithServer.syncCustomerFeedback(context);
DataSyncWithServer.syncTrackingData(context);
DataSyncWithServer.syncSalesInvoice(context);
DataSyncWithServer.syncSalesReturnInvoice(context);
DataSyncWithServer.syncPurchaseReturnInvoice(context);
DataSyncWithServer.syncStockTakeInvoice(context);
DataSyncWithServer.syncOrderManagementInvoice(context);
DataSyncWithServer.syncTesterManagementInvoice(context);
}
}
答案 0 :(得分:-1)
public void startOfflineDateAlarm() {
//* For Start Offline Sync Timer*//
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
/*alarmManager.set(AlarmManager.RTC_WAKEUP, Constant.offlineDataAlarmTime,
PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
*/
Log.e(TAG, "ALARM START");
long firstRunTime = calendar.getTimeInMillis();
long futureInMillis = 5 * 60 * 1000; //Repeat after 5 min
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
firstRunTime,
futureInMillis,
intentAlarm);
saveOfflineTables(getApplicationContext());
}