0

I am writing an app for sync purposes. And basically it should transfer data to a nas or a shared folder when it reachable. For example: When I come home my phone notices that 192.168.1.1 is now reachable and starts the sync process. How can this be achieved? I need something what continuously check whether the destination is reachable or not.

Any ideas ?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Peter Panne
  • 475
  • 1
  • 5
  • 15

3 Answers3

1

Have a background service and have it check every 5 minutes or so if the IP is reachable. If it is, start the sync process.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Every 5 minutes may dry battery soon. – Lucifer Mar 20 '14 at 06:31
  • 1
    Not really, not if you're just waking up and doing a bit of processing. The CPU uses almost no battery compared to the display and radios. To test- take a phone, charge it up. Put it in airplane mode, but write an app that takes a CPU wakelock and leave it on. It'll still have juice a few days later. Now if you turn on the screen you'd be dead pretty quickly. – Gabe Sechan Mar 20 '14 at 06:33
  • if you want to detect when network states change, can you use an intent? http://developer.android.com/reference/android/net/wifi/WifiManager.html – Justice Fist Mar 20 '14 at 06:43
  • http://stackoverflow.com/questions/5888502/android-wifi-how-to-detect-when-wifi-connection-has-been-established – Gabe Sechan Mar 20 '14 at 06:56
1

My idea is as follows:

Community
  • 1
  • 1
Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

The following is a service that can do it. To be more understandable, it uses a CountDownTimer to handle the looping with an anonymous thread to keep the network operations off of the main thread. Ideally, these should probably be combined in a handler. Also, unless a better synchronization approach is applied to the 'sycing' guard condition, TIME_OUT_MSECS should be kept fairly small compared CHECK_FREQ_MSECS to avoid race conditions on checking vs. setting it.

package com.example.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;

public class NetCheckService extends Service {

    private final static String TAG = "NetCheckService";

    private final static long CHECK_FREQ_MSECS = 300000L;  // 5 minutes 
    private final static int  TIME_OUT_MSECS = 5000;       // 5 seconds    
    private final static String IP_ADDR = "192.168.0.1";

    private Boolean syncing = false;
    CountDownTimer cdt = null;

    @Override
    public void onCreate() {       
        super.onCreate();

        Log.i(TAG, "Starting network check...");

        cdt = new CountDownTimer(Long.MAX_VALUE, CHECK_FREQ_MSECS) {
            @Override
            public void onTick(long millisUntilFinished) {

                // Don't check if we're already syncing.
                if (syncing)
                    return;

                Log.i(TAG, "Checking to see if IP address " + IP_ADDR + " is reachable...");
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            if (InetAddress.getByName(IP_ADDR).isReachable(TIME_OUT_MSECS))
                                doSync();
                            else
                                Log.i(TAG, "IP address is not reachable.");
                        } catch (UnknownHostException e) {
                            // Network wasn't reachable; ignore
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }

            @Override
            public void onFinish() {
                Log.i(TAG, "Nearly 300 million years have elapsed; this timer is tired.");
            }
        };

        cdt.start();
    }

    @Override
    public void onDestroy() {

        cdt.cancel();
        Log.i(TAG, "Timer cancelled.");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {       
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {       
        return null;
    }

    private void doSync() {
        syncing = true;
        Log.i(TAG, "Synching ...");
        // Do synchronization stuff
        syncing = false;
    }
}
scottt
  • 8,301
  • 1
  • 31
  • 41