Reputation: 13762
I use the following code to check the network availability while hitting remote service or accessing any web pages, for that i will call this below method every time before hitting web service, but i need any monitor which periodically monitor the web service at background and throw an alert network is not available, and if network is resume it net to throw an alert network is resumed , i don`t know how to achieve it.
public boolean isOnline(Context context)
{
boolean state=false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null) {
state=wifiNetwork.isConnectedOrConnecting();
}
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null) {
state=mobileNetwork.isConnectedOrConnecting();
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
state=activeNetwork.isConnectedOrConnecting();
}
return state;
}
Upvotes: 1
Views: 1119
Reputation: 3183
Here is an effective way of checking for an active connection: http://yue-gao.blogspot.com/2010/12/android-effective-way-to-test-internet.html
Upvotes: 1
Reputation: 56935
For that you need to implement the broadcast receiver, it will check the network connection in background and give alert when network has connectivity or not.
Please try below code.
1) Make one class which will extend brodcast receiver.
public class CheckInternetConnectionChangeReceiver extends BroadcastReceiver
{
public static boolean connectionStatus = false;
@Override
public void onReceive(Context context, Intent intent)
{
connectionStatus = CheckInternetConnection(context);
if(connectionStatus)
Toast.makeText(context, "Internet Connection Available", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, "Internet Connection Not Available", Toast.LENGTH_LONG).show();
}
public boolean CheckInternetConnection(Context context)
{
ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected())
{
return true;
}
else if (!mobile.isConnected())
{
return false;
}
else if (mobile.isConnected())
{
return true;
}
return false;
}
}
// Here connectionStatus is a one boolean variable which stored the true or false value according to network . If it is available it will stored true value other wise it will stored false value.
Now Paste below code in your android manifest file.
<receiver android:name=".CheckInternetConnectionChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Now when ever connection change it will call the brodcast receiver and store the appropriate value in the connnectionStatus variable.
Upvotes: 2