I'm developing an android application and I want to get a notification when the internet (wifi or packet data connection) connection is lost. On my approach I can get the status of the connection as:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
while having this in the Manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
How could I be notified automatically when the connection is lost?
Another way to say that you got disconnected is to say that your internet “dropped.” This means that it stopped working. You can also use “dropped” to describe a call that suddenly ended on its own, even when nobody hung up. Sorry, my internet keeps dropping.
Restarting your modem and router should be the first thing you do when encountering an internet signal issue. Don't skip this step! This is almost always what tech support will ask you to try first, as it often solves the problem. To restart your equipment, unplug the power cable for 60 seconds and plug it back in.
On an Android phone or tablet, open the Settings app and go to Network Connections to manage Wi-Fi, Bluetooth, and other networks such as mobile network and VPNs. Some newer versions call this Network & internet.
For WIFI you could register a broadcast receiver as:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
You can also register the receiver in the Manifest.
Then in your receiver:
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){
//do stuff
} else {
// wifi connection was lost
}
}
}
For any type of data connection listeners you could use the following receiver registered as:
<receiver android:name=".receiver.ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
and the in your ConnectivityReceiver
:
public class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
In the onReceive
method you could check if you have internet connectivity or not using this developer article.
use following code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import classes.NetworkUtil;
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
boolean IsConnected = NetworkUtil.getConnectivityStatusString(context);
// Toast in here, you can retrieve other value like String from NetworkUtil
// but you need some change in NetworkUtil Class
}
}
and NetworkUtil
is:
package classes;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
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 boolean getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
boolean status = false ;
if (conn == NetworkUtil.TYPE_WIFI) {
status = true;
} else if (conn == NetworkUtil.TYPE_MOBILE) {
status = true;
} else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
status = false;
}
return status;
}
}
and in manifest file:
<receiver
android:name="receiver.NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
and this permission:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With