在Marshmallow中以编程方式断开与wifi网络的连接

时间:2016-07-28 06:06:55

标签: android android-studio wifi android-6.0-marshmallow

 Disconnect from a wifi network programmatically in Marshmallow 

无法在marshmallow中以编程方式断开与wifi网络的连接,无法找到任何解决方案。如果任何人有任何解决方案将是巨大的帮助。谢谢你

1 个答案:

答案 0 :(得分:0)

<强>更新

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    wm.disconnect();

还为运行Android 6.0的设备设置了在Manifest和运行时更改Wifi状态的权限

Manifest.xml

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

在运行时检查权限:

if (ActivityCompat.checkSelfPermission(SmsOtpCheck.this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CHANGE_WIFI_STATE}, CODE);

批准或拒绝许可后:

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case CODE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    //PERMISSION GRANTED
                else
                    Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
        }
    }