简体   繁体   English

wifi连接的BroadcastReceiver无法正常工作

[英]BroadcastReceiver for wifi connection don't work correctly

I want to set my wifi off after a certain time when wifi is not connected. 我想在未连接wifi的一定时间后关闭wifi。 The probleme that occur to me is when i remove the wifi connection onreceive got call but say the network still connected. 我发生的问题是当我删除wifi连接onreceive收到呼叫但说网络仍然连接时。 I don't understand why its connected didn't the state just change? 我不明白为什么其连接状态没有改变? Did the state change so much fast that it didn't have time to catch it?? 状态变化是如此之快以至于没有时间去抓住它吗? Any help will be appreciate. 任何帮助将不胜感激。

Here my receiver 这是我的接收器

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");
        }
    }

I have tested your code on 2 devices (GS3 and Samsung Charge). 我已经在2种设备(GS3和Samsung Charge)上测试了您的代码。 It worked fine. 工作正常。 The state changed after I toggled wifi setup. 切换wifi设置后,状态发生了变化。 My code is on github, here . 我的代码在github上, 在这里 And below is the log: 下面是日志:

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

I think it might have something to do with your phone or ROM. 我认为这可能与您的手机或ROM有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM