1

I'm saving data to my embedded DB(Realm.io) and I want to continuous check if there is internet connection and if connection is OK check if there is data to sync with server, then synchronize data, remove synchronized data from embedded DB and set the state to "synchronized" until another data is added to my embedded DB.

It seems to check internet connection async. I need to implement BroadcastReciever:

Internet listener Android example

But I need to do this in any activity because maybe I retrieve internet connection when I change activity, I thought adding this broadcast reciever in every activity but looks dirty, how I can do this in a clean way? Is it possible to create a thread that have a broadcast reciever and if inet connection is ok persists data, and keep it alive until application is closed?

Now I'm Trying to do a permanent IntentService to check if DB need to be synchronized and if internet is available sync DB:

public class MyIntentService extends IntentService {
private PreferenceController preferenceController;

public MyIntentService() {
    super("MyIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    preferenceController = new PreferenceController(this);

    int i = 0;
    while (i >= 0) {
        Log.d("ISERVICE", "checking number: " + i);
        checkUpdate();
        waitInSeconds(300);
        i++;
    }
}

private void waitInSeconds(int seconds) {
    try {
        Thread.sleep(seconds * 1000);

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

private void checkUpdate() {
    if (preferenceController.getBooleanPreference("SYNC_DB", false)) { // if DB need to be synchronized
        if (isOnline()) { // if connected to internet
            syncDatabases();
        }
    }
}

private void syncDatabases() {
    // Do stuff to synchronize realm.io with server
}

/*
    Recieve Broadcast if internet is available again and call checkUpdate()
 */

public boolean isOnline() {

    Runtime runtime = Runtime.getRuntime();
    try {

        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int exitValue = ipProcess.waitFor();
        return (exitValue == 0);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return false;
}

}

Community
  • 1
  • 1
MikeOx
  • 167
  • 1
  • 1
  • 16

1 Answers1

0

Use IntentService to send data from your local database to server.

By the help of the IntentService you also check that the internet present. Check IntentService run or not at your main activity when application restarts, if not then start IntentService again.

If you want to do this also when application is not in background and not in foreground then use AlarmReceiver to register IntentService which makes it run continously whether you Force Stop or delete your application.

Chirag Arora
  • 816
  • 8
  • 20
  • Let me try it and I comment results – MikeOx Jul 05 '16 at 13:47
  • sure i am always happy to help – Chirag Arora Jul 05 '16 at 13:48
  • I found some comments about IntentService saying that is not recommended to "permanent" do something because Android kills it after some time, like 30 mins, that is better using service, but also that service is designed for short tasks(doesn't matter how many times) and IntentService for long tasks but (but just to do it once), now I'm a bit confused, my App will be running for more than 7 h so I need something secure to synchronize data with DB, Can be good practice doing a service that checks if intentservice is alive and if not restart it? – MikeOx Jul 05 '16 at 14:50
  • If your app run in background or foreground then intent service is best, yes you also need to check that intent service is alive or not if not then restart that.. I gave you suggestion to run intent service with alarm receiver if your app kills from background and you want to synchronize your data from your db then use that.. – Chirag Arora Jul 06 '16 at 04:47
  • My app will be always opened, so im not sure if alarm android is good for me – MikeOx Jul 06 '16 at 15:24
  • 1
    If your app is always opened then Intent service is best – Chirag Arora Jul 06 '16 at 16:36