AlarmManager首先开火一次然后每秒反复点火......为什么?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AlarmManager首先开火一次然后每秒反复点火......为什么?相关的知识,希望对你有一定的参考价值。
我正在使用离线任务,我必须每5分钟在backgourd中调用Webservice
。所以我正在使用AlarmManager
。
第一次AlarmManager
开火时间(开始5分钟后),但是比起它在几秒钟内开火,我不知道为什么?
我只需要每5分钟一次火警AlarmManager ......
请帮忙..
这是启动计时器的代码..
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);
}
}
答案
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());
}
以上是关于AlarmManager首先开火一次然后每秒反复点火......为什么?的主要内容,如果未能解决你的问题,请参考以下文章
android AlarmManager 在凌晨12点 手机实机无法达成的问题?
Android:如何每 15 分钟使用 AlarmManager 重复一次服务,但只在上午 8:00 到晚上 18:00 运行?