我正在开发一个Android应用程序,我想在互联网(wifi或分组数据连接)连接丢失时收到通知。在我的方法中,我可以获得连接的状态:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
在Manifest中有这个:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
如何在连接丢失时自动收到通知?
答案 0 :(得分:10)
对于WIFI,您可以将广播接收器注册为:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
您也可以在Manifest中注册接收器。
然后在你的接收器中:
@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
}
}
}
对于任何类型的数据连接侦听器,您可以使用以下接收器注册为:
<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>
和您的ConnectivityReceiver
:
public class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
在onReceive
方法中,您可以使用this developer article检查是否有互联网连接。
答案 1 :(得分:3)
使用以下代码:
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
}
}
和NetworkUtil
是:
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;
}
}
并在清单文件中:
<receiver
android:name="receiver.NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
和此权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
答案 2 :(得分:0)
您需要设置广播接收器。
将这些添加为意图过滤器。
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
答案 3 :(得分:-2)
您应该使用BroadcastReceivers来满足您的要求。创建一个侦听网络更改的BroadcastReceiver。
如果网络发生任何变化,它将自动触发事件,您可以执行相关操作。