监听WIFI状态

发布于 2025-01-08 11:35:13 字数 1229 浏览 1 评论 0原文

我想设置侦听器来侦听无线状态,任何人都可以帮助我编写代码

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

......

 TelephonyManager wTelephonyManager;

     wTelephonyManager=(TelephonyManager)getSystemService(Context.WIFI_SERVICE);
    wTelephonyManager.listen(new PhoneL(),PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

//这是我使用的事件,我想侦听wifi变化,上面的代码全部在onCreate {}

class PhoneL extends PhoneStateListener

{   






    public void onWifiStateChanged(int state, String nesto)
    {
         mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //mWifi.isConnectedOrConnecting()
             if(mWifi.isConnectedOrConnecting())
             {
                 Toast.makeText(WifiActivity.this,"Ima WIFI",Toast.LENGTH_LONG).show();     
             }
             else
             {
                 Toast.makeText(WifiActivity.this,"! NEMA WIFI",Toast.LENGTH_LONG).show(); 
             }


    }

}

//谁能帮我创建一个侦听器来侦听 wifi 状态并检查 wifi 是否已连接或是否已连接 我想通过 3g/4g 启用数据包流量

I want to set listener to listen on wireless state,can anyone help me with my code

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

...

 TelephonyManager wTelephonyManager;

...

     wTelephonyManager=(TelephonyManager)getSystemService(Context.WIFI_SERVICE);
    wTelephonyManager.listen(new PhoneL(),PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

//here is the event that i use that i want to listen for wifi change, and the above code is all in onCreate{}

class PhoneL extends PhoneStateListener

{   






    public void onWifiStateChanged(int state, String nesto)
    {
         mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //mWifi.isConnectedOrConnecting()
             if(mWifi.isConnectedOrConnecting())
             {
                 Toast.makeText(WifiActivity.this,"Ima WIFI",Toast.LENGTH_LONG).show();     
             }
             else
             {
                 Toast.makeText(WifiActivity.this,"! NEMA WIFI",Toast.LENGTH_LONG).show(); 
             }


    }

}

//Can anyone please help me creating a listener that will listen to the wifi state and check if wifi is connected or connecting if is not i want to enable data packet traffic through 3g/4g

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最单纯的乌龟 2025-01-15 11:35:13

我也遇到了这个问题,这是我解决的方法。

在我的活动 onCreate(..) 中,我这样做了

this.registerReceiver(mWifiStateChangedReceiver,new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));

,另外我还以这种方式创建了成员“mWifiStateChangedReceiver”

private BroadcastReceiver mWifiStateChangedReceiver = new BroadcastReceiver()
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        // TODO Auto-generated method stub

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

    }
};

如您所见,当我得到 WifiManager.WIFI_STATE_ENABLED 时,我还测试了网络是否真正连接,因为启用 WiFi 并不意味着已连接。至少这是我的猜测,这就是为什么我要等到网络真正连接起来。

I came across that problem too, here's how I solved it.

In my activity onCreate(..) I did

this.registerReceiver(mWifiStateChangedReceiver,new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));

and additionally i created the member 'mWifiStateChangedReceiver' this way

private BroadcastReceiver mWifiStateChangedReceiver = new BroadcastReceiver()
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        // TODO Auto-generated method stub

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

    }
};

As you can see, when I get WifiManager.WIFI_STATE_ENABLED I additionally test if the network is really connected, because a enabled WiFi doesn't mean it's connected. At least this was my guess, that's why I'm waiting till the network is really connected.

此岸叶落 2025-01-15 11:35:13

如果您想收听信号强度,也可以收听:

WifiManager.RSSI_CHANGED_ACTION

If you want to listen to signal strength you can also listen at:

WifiManager.RSSI_CHANGED_ACTION

想你只要分分秒秒 2025-01-15 11:35:13

WiFi 未连接但出现问题时,Android 系统要求切换到数据流量
如果您在屏幕关闭时关闭 WiFi,它会不断地在 WiFi 和 3G 网络之间切换,并且如果您选择了在后台发送数据的应用程序,它将使用数据连接,如果您没有无限数据计划,则成本会更高.
另一个问题是,当您位于 WiFi 信号不太强的区域时,它会不断变为数据流量,并且您会遇到浏览或其他问题。
第三个问题是电池,据我所知,使用 WiFi 连接比使用数据更好(电池方面),有一个附加组件不会在屏幕关闭时关闭 WiFi。(即使我这样做)因为关闭并打开 WiFi 是最昂贵的(电池),所以始终打开。所以把它留给android系统吧。

The android system asks to switch to Data Traffic when WiFi is not connected but there is an issue.
If you turn off WiFi when screen is off, it will constantly change between WiFi and 3G network and if you have selected apps to send data in the background, it will use the data connection which is costlier if you don't have unlimited data plan.
Another issue is when you are in a area with not so strong WiFi signal, it will constantly change to Data Traffic and you will have issues with browsing or whatever.
Third issue is battery as from what I've heard it's better(battery wise) to use the WiFi connection than the data, with one Add-on don't turn off WiFi when the screen is off.(even, I do so)because turning it off and on WiFi is most costly(battery) that to be always On. so therefore leave it to the android system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文