If you are
developing an Android app you may already fetching information from internet.
While doing so there is a chance that internet connection is not available on
users handset.
Hence it's always a
good idea to check the network state before performing any task that requires
internet connection.
You might also want
to check what kind of internet connection is available in handset. For example
is wifi currently enabled? or is mobile data network is connected.
Check Internet Connection:
Here is a simple
code snippet that will help you identify what kind of internet connection a
user has on her device.
First we need
following permission in order to access network state. Add following permission
to your AndroidManifest.xml file.
Permissions required
to access network state:
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
Now check following
utility class NetworkUtil. It has
method getConnectivityStatus which returns an int constant depending
on current network connection. If wifi is enabled, this method will
return TYPE_WIFI. Similarly for mobile data network is
returns TYPE_MOBILE. You got the idea!!
There is also
method getConnectivityStatusString which returns current network
state as a more readable string.
You can use this utility class in your android app to check the network state of the device at any moment. Now this code will return you the current network state whenever the utility method is called. What if you want to do something in your android app when network state changes?
Let's say when Wifi is disabled, you need to put your android app service to sleep so that it does not perform certain task. Now this is just one usecase. The idea is to create a hook which gets called whenever network state changes. And you can write your custom code in this hook to handle the change in network state
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class NetworkUtil { public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 2; public static int TYPE_NOT_CONNECTED = 0; public static int getConnectivityStatus(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI; if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE; } return TYPE_NOT_CONNECTED; } public static String getConnectivityStatusString(Context context) { int conn = NetworkUtil.getConnectivityStatus(context); String status = null; if (conn == NetworkUtil.TYPE_WIFI) { status = "Wifi enabled"; } else if (conn == NetworkUtil.TYPE_MOBILE) { status = "Mobile data enabled"; } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { status = "Not connected to Internet"; } return status; } } |
You can use this utility class in your android app to check the network state of the device at any moment. Now this code will return you the current network state whenever the utility method is called. What if you want to do something in your android app when network state changes?
Let's say when Wifi is disabled, you need to put your android app service to sleep so that it does not perform certain task. Now this is just one usecase. The idea is to create a hook which gets called whenever network state changes. And you can write your custom code in this hook to handle the change in network state
Broadcast Receiver to handle changes in Network state:
You can easily
handle the changes in network state by creating your own Broadcast Receiver.
Following is a broadcast receiver class where we handle the changes in network.
Check onReceive() method.
This method will be called when state of network changes. Here we are just
creating a Toast message and displaying current network state. You can write
your custom code in here to handle changes in connection state.
1 2 3 4 5 6 7 | public class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { String status = NetworkUtil.getConnectivityStatusString(context); Toast.makeText(context, status, Toast.LENGTH_LONG).show(); } } |
Once we define our
BroadcastReceiver, we need to define the same in AndroidManifest.xml file. Add
following to your manifest file.
1 2 3 4 5 6 7 8 9 10 11 12 | <application ...> ... <receiver android:name=".NetworkChangeReceiver" android:label="NetworkChangeReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> </intent-filter> </receiver> ... </application> |
! Done
Thanks to http://viralpatel.net/blogs/android-internet-connection-status-network-change/
No comments:
Post a Comment