警报管理器不会定期在服务器中推送数据以获得牛轧糖

时间:2017-04-07 13:09:22

标签: android

我正在使用AlarmManager来调用PendingIntent。触发WakefulBroadcastReceiver定期推送服务器中的数据。但是对于牛轧糖,它不会在周期性时间推进。还有太多的电池耗尽。

my code is here->

public static void startSyncAlarm(String enteredTime) {
    // Construct an intent that will execute the AlarmReceiver
    Intent intent = new Intent(context, SyncDbToServerAlarmReceiver.class);
    intent.putExtra(Constants.ENTERED_TIME, enteredTime);
    // Create a PendingIntent to be triggered when the alarm goes off
    final PendingIntent pIntent = PendingIntent.getBroadcast(context, SyncDbToServerAlarmReceiver.REQUEST_CODE,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // alarm needs to be set after 5 minutes of entered time
    // convert entered time to milliseconds
    // create Date object using string time format with using Simple date format
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    try {
        Date enteredDate = format.parse(enteredTime);
        long enteredTimeInMillis = enteredDate.getTime();
        long intervalMillis = 30 * 60 * 1000; // 30 minutes gap
        long triggerAtMillis = enteredTimeInMillis + intervalMillis;
        Log.d(TAG, "start alarm, enteredTimeMillis = " + enteredTimeInMillis + "\ntriggerAtMillis = " + triggerAtMillis);
    //long firstMillis = System.currentTimeMillis(); // alarm is set right away
        syncDbAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        // Setup periodic alarm every 5 minutes
        syncDbAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis/*firstMillis*/,
                intervalMillis, pIntent);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案