0

I am trying to crate a service in my application which backup data to server on daily basis. I have tried to do so using Alarm Manager, wakeful Broadcast Receiver and service. here is my code..

private void setAlarm(long time) {
    int id = (int) System.currentTimeMillis();

    Intent intent = new Intent(BackupController.ACTION_SCHEDULE);
    // In reality, you would want to have a static variable for the request code instead of 192837
    PendingIntent sender = PendingIntent.getBroadcast(BackupSettingsActivity.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Get the AlarmManager service
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    try {

        am.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_DAY, sender);

    } catch (ParseException e) {
        e.printStackTrace();
    }

}

The BackupController.ACTION_SCHEDULE is received by following broadcast receiver..

public class ConnectivityReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(BackupController.ACTION_SCHEDULE)) {
        boolean IS_NETWORK_AVAILABLE = AppUtils.haveNetworkConnection(context);
        Log.d("Alarm Receiver", "onReceive called");

        if (IS_NETWORK_AVAILABLE) {
            performOffLineTask(context);
        }

    }
}


private void performOffLineTask(Context context) {
    SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE);
    boolean b = preferences.getBoolean(AppUtils.IS_SERVICING_RUNNING, false);
    if (!b) {
        Intent intent = new Intent(context, BackupService.class);
        context.getApplicationContext().startService(intent);
    }
}

}

Now the problem is on device with API 19+, ConnectivityReceiver is not triggered on exact time (delayed by few minutes) when it was scheduled by alarm manager. Some times it results to not calling the BackupService at all when device is idle or app is killed.

Is there any way to start a repetitive service on daily basis on exact time even if device is ideal of application is killed.

I have been through these links:

Alarm manager not triggering alarms at exact time in android and alarm and notificaiton not triggered at specified time

Community
  • 1
  • 1
Satya
  • 149
  • 1
  • 6

0 Answers0