Android的,我想设置监听器无线监听状态,谁能帮助我与我的code我的、我想、监听器、谁能

2023-09-04 12:18:04 作者:淡紫青云

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

//这里是我使用,我想监听无线的变化的情况下,和上面的code是所有的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状态,检查无线连接或者连接,如果不是我希望能够通过3G / 4G的数据包流量

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

推荐答案

Android系统的要求,切换到数据通信无线未接通,但有一个问题。 如果您关闭WiFi,当屏幕关闭时,它会不断的WiFi和3G网络之间改变,如果你选择的应用程序在后台发送数据时,它会使用数据连接是昂贵的,如果你没有无限数据计划。照片 另一个问题是,当你在没有那么强的WiFi信号范围,它就会不断地变化数据流量,你将与浏览或其他问题。 第三个问题是电池从我听说这是更好(电池WISE)使用比数据的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.