Reputation:
How can I detect if WiFi
is switched on to decide whether to transfer data to the server via android client or not?
On the android device using web services, first the android device checks that WiFi
is on or off, and when the WiFi
is switched off data should be stored in database or some other resource and as soon as the WiFi is turned on/or become available data should be started to transfer to the server.
What should be used to implement this?
Upvotes: 1
Views: 1239
Reputation: 13761
You can check whether WiFi
is turned on or not. Just include a code like this:
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (ni.isConnected())
Log.d("WifiOn", "Wifi is connected");
else
Log.d("WifiOn", "Wifi is not connected");
In order to make this work, don't forget to put this line in your AndroidManifest.xml
file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
If you don't have Wifi enabled, you can store the data in a local SQLite
database and as soon as you detect a configuration change, store the data through net. To know how to detect a configuration change, you might look at this question:
Upvotes: 5