广播接收器上android.net.conn.CONNECTIVITY_CHANGE多次调用 [英] BroadcastReceiver on android.net.conn.CONNECTIVITY_CHANGE called multiple times
本文介绍了广播接收器上android.net.conn.CONNECTIVITY_CHANGE多次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的清单以下
<接收机器人:名字=。receiver.WifiReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.net.conn.CONNECTIVITY_CHANGE/>
&所述; /意图滤光器>
< /接收器>
和以下的BroadcastReceiver:
公共类WifiReceiver扩展广播接收器{
私有静态字符串标记= makeLogTag(WifiReceiver.class);@覆盖
公共无效的onReceive(上下文的背景下,意图意图){ ConnectivityManager connectivityManager =(ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
的NetworkInfo NETWORKINFO = NULL;
如果(connectivityManager!= NULL){
NETWORKINFO =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); LOGD(TAG的连接信息:+ NETWORKINFO);
} 如果(NETWORKINFO = NULL&放大器;!&安培; networkInfo.isConnected()){
// TODO:为什么这个被多次调用和处理进度重装
LOGD(TAG,有Wifi连接,并连接);
}其他
LOGD(TAG,没有无线连接或没有连接);
}
当我从手机切换到接收器获得多次调用(也没问题),但在WiFi
如果(NETWORKINFO = NULL&放大器;!&安培; networkInfo.isConnected())
分支计算为true的所有4次
解决方案
公共类NetworkChangeReceiver扩展广播接收器{
上下文mContext;@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
mContext =背景;
字符串状态= NetworkUtil.getConnectivityStatusString(背景); Log.e(接收器,+状态); 如果(status.equals(未连接互联网)){
Log.e(接收器,不connction); //你的code当互联网失去了
}其他{
Log.e(接收器,连接到互联网); //你的code当互联网连接回来
}}
}
和使用要求检查WiFi或互联网conenctivity失去
公共类NetworkUtil {公共静态INT TYPE_WIFI = 1;
公共静态INT TYPE_MOBILE = 2;
公共静态INT TYPE_NOT_CONNECTED = 0;公共静态INT getConnectivityStatus(上下文的背景下){ ConnectivityManager厘米=(ConnectivityManager)上下文
.getSystemService(Context.CONNECTIVITY_SERVICE); 的NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 如果(NULL!= activeNetwork){ 如果(activeNetwork.getType()== ConnectivityManager.TYPE_WIFI)
返回TYPE_WIFI; 如果(activeNetwork.getType()== ConnectivityManager.TYPE_MOBILE)
返回TYPE_MOBILE;
}
返回TYPE_NOT_CONNECTED;
}公共静态字符串getConnectivityStatusString(上下文的背景下){ 康涅狄格州INT = NetworkUtil.getConnectivityStatus(背景); 字符串状态= NULL;
如果(康涅狄格州== NetworkUtil.TYPE_WIFI){
//状态=WLAN网络;
状态=Internet连接可用;
}否则如果(康恩== NetworkUtil.TYPE_MOBILE){
//状态=移动数据已启用;
状态=Internet连接可用;
}否则如果(康恩== NetworkUtil.TYPE_NOT_CONNECTED){
状态=未连接互联网;
}
返回状态;
}
}
和你androidmanifest文件中添加此
<接收
机器人:名字=。NetworkChangeReceiver
机器人:标签=NetworkChangeReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.net.conn.CONNECTIVITY_CHANGE/>
<作用机器人:名字=android.net.wifi.WIFI_STATE_CHANGED/>
&所述; /意图滤光器>
< /接收器>
我希望这是对你的工作以及
I have the following in my manifest
<receiver android:name=".receiver.WifiReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
and the following BroadcastReceiver:
public class WifiReceiver extends BroadcastReceiver {
private static String TAG = makeLogTag(WifiReceiver.class);
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = null;
if (connectivityManager != null) {
networkInfo =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
LOGD(TAG, "connectivity info:" + networkInfo);
}
if(networkInfo != null && networkInfo.isConnected()) {
//TODO: see why this is called multiple times and handle schedule reloading
LOGD(TAG, "have Wifi connection and is connected");
}else
LOGD(TAG, "don't have Wifi connect or it isn't connected");
}
When i switch from mobile to wifi the receiver get called multiple times (no problem there) but the
if(networkInfo != null && networkInfo.isConnected())
branch evaluates to true all 4 times
解决方案
public class NetworkChangeReceiver extends BroadcastReceiver {
Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
String status = NetworkUtil.getConnectivityStatusString(context);
Log.e("Receiver ", "" + status);
if (status.equals("Not connected to Internet")) {
Log.e("Receiver ", "not connction");// your code when internet lost
} else {
Log.e("Receiver ", "connected to internet");//your code when internet connection come back
}
}
}
and use this call for check wifi or internet conenctivity lost
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 String getConnectivityStatusString(Context context) {
int conn = NetworkUtil.getConnectivityStatus(context);
String status = null;
if (conn == NetworkUtil.TYPE_WIFI) {
//status = "Wifi enabled";
status="Internet connection available";
} else if (conn == NetworkUtil.TYPE_MOBILE) {
//status = "Mobile data enabled";
status="Internet connection available";
} else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
status = "Not connected to Internet";
}
return status;
}
}
and add this in your androidmanifest file
<receiver
android:name=".NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
i hope it's work for you and
这篇关于广播接收器上android.net.conn.CONNECTIVITY_CHANGE多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文