Search code examples
androidandroid-networkingandroid-syncadapter

Restricting Android syncing to WIFi


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.)?


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"/>