问题
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?
回答1:
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.
回答2:
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" />
回答3:
You need to set up a broadcast receiver.
Add these as intent filters.
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
回答4:
You should use BroadcastReceivers for your requirement. Create a BroadcastReceiver which listen for network changes.
Incase of any change in the network it will automatically fire the event and you can perform your operations regarding it.
来源:https://stackoverflow.com/questions/21226776/android-how-to-get-indicate-when-the-internet-connection-is-lost