监听WIFI状态 [英] Listening WIFI state

查看:224
本文介绍了监听WIFI状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置侦听器以侦听无线状态,任何人都可以通过我的代码帮助我

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

//这是我要监听wifi变化的事件,以上代码全部在onCreate {}中

//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(); 
             }


    }

}

//任何人都可以帮助我创建一个侦听器,该侦听器将侦听wifi状态并检查wifi是否已连接,或者是否要连接wifi,我想通过3g / 4g启用数据包流量。 b $ b

//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

推荐答案

WiFi时,Android系统要求切换到数据流量 未连接,但有问题。

如果在屏幕关闭时关闭WiFi,则它将在WiFi和3G网络之间不断变化,并且如果您选择了要发送的应用

另一个问题是,当您处于WiFi信号不太强的区域时,它将持续不断地使用数据连接。更改为数据流量,您将在浏览或其他方面遇到问题。

第三个问题是电池,因为据我所知,使用WiFi连接要比dat更好(在电池方面) a,在没有一个附加组件的情况下,请勿在屏幕关闭时关闭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.

这篇关于监听WIFI状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆