我希望在未连接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");
}
}
答案 0 :(得分:1)
我已在2台设备(GS3和Samsung Charge)上测试了您的代码。它工作正常。 切换wifi设置后,状态发生了变化。 我的代码在github上,here。以下是日志:
06-17 15:50:46.151: V/msg(12488): onReceive WifiDataReceiver
06-17 15:50:46.151: V/msg(12488): mWifi : DISCONNECTED
06-17 15:50:56.151: V/msg(12488): Disconnect WifiDataReceiver
06-17 15:50:56.221: V/msg(12488): onReceive WifiDataReceiver
06-17 15:50:56.221: V/msg(12488): mWifi : DISCONNECTED
06-17 15:51:06.232: V/msg(12488): Disconnect WifiDataReceiver
06-17 15:51:06.292: V/msg(12488): onReceive WifiDataReceiver
06-17 15:51:06.302: V/msg(12488): mWifi : DISCONNECTED
06-17 15:51:16.293: V/msg(12488): Disconnect WifiDataReceiver
06-17 15:51:39.678: V/msg(12488): onReceive WifiDataReceiver
06-17 15:51:39.678: V/msg(12488): mWifi : CONNECTED
06-17 15:51:39.678: V/msg(12488): connected
06-17 15:51:39.708: V/msg(12488): onReceive WifiDataReceiver
06-17 15:51:39.708: V/msg(12488): mWifi : CONNECTED
06-17 15:51:39.708: V/msg(12488): connected
06-17 15:51:39.908: V/msg(12488): onReceive WifiDataReceiver
06-17 15:51:39.918: V/msg(12488): mWifi : CONNECTED
06-17 15:51:39.918: V/msg(12488): connected
06-17 15:51:39.918: V/msg(12488): onReceive WifiDataReceiver
06-17 15:51:39.918: V/msg(12488): mWifi : CONNECTED
06-17 15:51:39.918: V/msg(12488): connected
我认为这可能与您的手机或ROM有关。