我想在 wifi 未连接一段时间后关闭我的 wifi。我遇到的问题是当我删除 wifi 连接时
onreceive
接到电话但说网络仍然连接。我不明白为什么它的连接状态没有改变?是不是状态变化太快以至于来不及捕捉??任何帮助将不胜感激。
这是我的接收器
class WifiDataReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
Log.v("msg", "onReceive WifiDataReceiver");
Log.v("msg", "mWifi : " + mWifi);
cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
mWifi = cm.getNetworkInfo(1).getState();
if (mWifi != NetworkInfo.State.CONNECTED){
try {
Thread.sleep(disconnectionTimeout);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wifiManager.setWifiEnabled(false);
Log.v("msg", "Disconnect WifiDataReceiver");
} else {
Log.v("msg", "connected");
}
}