Broadcast receiver code to check internet connectivity change:
public class BroadCastDetecter extends BroadcastReceiver {
public static boolean internet_status = false;
public static void checkInternetConenction(Context context) {
internet_status = false;
ConnectivityManager check = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (check != null) {
NetworkInfo[] info = check.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
{
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
internet_status = true;
}
}
if(internet_status)
{
//do what you want to if internet connection is available
}
}
}
@Override
public void onReceive(Context context, Intent intent)
{
try {
checkInternetConenction(context);
}catch(Exception e){
}
}
}
add this in manifest file:
<receiver android:name=".BroadCastDetecter">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>