0

I am using sync adapter to sync between my mobile device to my central server. When I add a record to my sqlite DB on the device, I am using requestSync to sync with server and it is working well. Even when there is no internet connection with server, I am able to force re-sync later by catching the ParseException and incrementing syncResult.stats.numParseExceptions in the SyncAdapter's' onPerformSync.

My problem is if i switch-off my device before sync is complete. No sync is attempted when i turn device on again. What can I do to ensure sync is attempted when I turn device on again?

My code:

UI activity --> on Save Button Click event handler

ContentResolver.requestSync(createDummyAccount() DatabaseContract.AUTHORITY , settingsBundle);

//SyncAdapter -->> onPerformSync()

Client client = ClientBuilder.newClient();

client.property(ClientProperties.CONNECT_TIMEOUT, 6000);

WebTarget target = client.target(SERVICE_URL);

try {
     String callResult = target
     .request(MediaType.APPLICATION_XML)
     .put(Entity.entity(form,
        MediaType.APPLICATION_FORM_URLENCODED_TYPE),
        String.class);
    System.out.println("response: " + callResult);
} catch (ProcessingException pe) {
    syncResult.stats.numIoExceptions++;
    syncResult.delayUntil = 60;
}
gkinu
  • 81
  • 2
  • 9

1 Answers1

0

You can have look on this SO post

Android BroadcastReceiver, auto run service after reboot of device

by Dixit Patel

and start your service once you received the broadcast receiver. In this you have to register with android.intent.action.BOOT_COMPLETED to receive broadcast

Community
  • 1
  • 1
Rahul Khurana
  • 7,582
  • 5
  • 29
  • 54
  • I am not getting it clear here. You want me to implement a BroadcastReceiver? – gkinu Nov 19 '16 at 12:28
  • Sorry, but i am a bit confused. I already had a bound service - SyncService - declared in the manifest to instantiate the SyncAdapter. Is this not enough ... or which service will the BroadCast receiver above start? – gkinu Nov 19 '16 at 12:57
  • the broadcast receiver is to receive the device boot event and then run your service again – Saik Caskey Feb 16 '17 at 11:09