如何检查android wifi连接?

时间:2017-01-23 18:08:40

标签: android

我想检查它是否已连接到WiFi。如果连接,手机会保持静音。当程序运行时,应用程序停止工作

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

1 个答案:

答案 0 :(得分:0)

你可以直接使用这个方法如果连接然后它将返回true其他错误记住它将无法在模拟器上工作请在真实设备上测试..谢谢

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