Limit Android sync with WIFi

I am new to Android and trying to write SyncAdapter - is it possible to limit SyncAdapter only synchronization when the network is WiFi (and not 3G, etc.)?

+4
source share
2 answers

you can check wifi how

  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 to the manifest as

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

The answer above is good, but I would add an HttpHead-Request (HTTPGet is also possible, but you get more data than required), which calls for example. google.com so you can be sure that you’re really connected to the Internet. The above solution just checks to see if you have network access, but does not guarantee that you have inernet access.

+2
source

Source: https://habr.com/ru/post/1442960/


All Articles