Question

I am new to Android and am trying to write a SyncAdapter - is it possible to restrict the SyncAdapter to only perform a sync when the network is WiFi (and not 3G etc.)?

Was it helpful?

Solution

you can make check for wifi as

    ConnectivityManager cm = (ConnectivityManager) YourActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();

    if(info.getType()==ConnectivityManager.TYPE_WIFI)
    {
        System.out.println("WiFi Connected");
    }
    else
    {
            System.out.println("WiFi not connected");
    }

and also add permission in manifest as

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

OTHER TIPS

The answer above is good, but I would add a HttpHead-Request (HTTPGet is also possible but you receive more data than needed) that is calling e.g. google.com so you can be sure that you are really connected to the internet. The above posted solution just checks if you have network access but does not guarantee that you have inernet access.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top