简体   繁体   English

如何检查android wifi连接?

[英]How to check android wifi connection?

I want to check if it is connected to WiFi.我想检查它是否连接到WiFi。 If connected, the phone is silent.如果已连接,电话将静音。 When the program is running application stopped working当程序运行时应用程序停止工作

 ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (mWifi.isConnected()) {
                final AudioManager audioManager=
                        (AudioManager)getSystemService(getApplicationContext().AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

            }
            else
            {
                final AudioManager audioManager=
                        (AudioManager)getSystemService(getApplicationContext().AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            }

you can directly use this method if connect then it will return true else false remember it will not work on Emulator please test it on real device .. Thanks你可以直接使用这个方法,如果连接然后它会返回真否则假记住它不能在模拟器上运行请在真实设备上测试它..谢谢

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
        if(netInfo != null && netInfo.isConnected()) {
            //we are connected to a network
            return true;
        }
        else
            return false;
    }

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

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