我正在使用广播接收器来提供互联网。 这是代码
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(DashBoardActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
if(currentNetworkInfo.isConnected()){
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
}
}
};
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
this.registerReceiver(this.mConnReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
Toast.makeText(getApplicationContext(), "In resume", Toast.LENGTH_LONG).show();
}
当我打开WiFi onReceive只被调用一次但是当我关闭WiFi onReceive被调用两次因为弹出或上面代码中使用的Toast出现两次。
任何人都可以帮忙解决这个奇怪的错误吗?
答案 0 :(得分:0)
public void onReceive(Context context, Intent intent) {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetworkInfo = connManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isAvailable()) {a
Toast.makeText(getApplicationContext(),"Connected",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:0)
我猜你在活动暂停时没有取消注册接收者。
将此添加到您的活动
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
this.unregisterReceiver(this.mConnReceiver);
Toast.makeText(getApplicationContext(), "In pause", Toast.LENGTH_LONG).show();
}
答案 2 :(得分:0)
据我所知,当我们可能有多个WiFi状态 设备中的关闭wifi。
使用以下代码检查wifi的状态并采取相应的措施:
int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
switch (extraWifiState)
{
case WifiManager.WIFI_STATE_DISABLED:
case WifiManager.WIFI_STATE_DISABLING:
enableUI(false);
break;
case WifiManager.WIFI_STATE_ENABLED:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
while(conMan.getActiveNetworkInfo() == null || conMan.getActiveNetworkInfo().getState() != NetworkInfo.State.CONNECTED)
{
try
{
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
update();
enableUI(true);
break;
case WifiManager.WIFI_STATE_ENABLING:
break;
case WifiManager.WIFI_STATE_UNKNOWN:
break;
}
}
可能会帮助您找到问题的解决方案。