本文介绍了如何在Android 8.0(Oreo)中以编程方式关闭wifi热点(setWifiApEnabled不再支持该版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我引用了在Android 8.0中打开热点的代码,这是可行的.但是我不知道如何禁用它

I reference the code to turn on hotspot in Android 8.0, it is work. But I have no idea about how to disable it

@RequiresApi(api = Build.VERSION_CODES.O)
private void turnOnHotspot(){
    WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback(){

        @Override
        public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
            super.onStarted(reservation);
            Log.d(TAG, "Wifi Hotspot is on now");
        }

        @Override
        public void onStopped() {
            super.onStopped();
            Log.d(TAG, "onStopped: ");
        }

        @Override
        public void onFailed(int reason) {
            super.onFailed(reason);
            Log.d(TAG, "onFailed: ");
        }
    },new Handler());
}

我想从LocalOnlyHotspotReservation使用close()方法,但是如何从外部获取预订实例,例如reservation.close();

I want to use close() method from LocalOnlyHotspotReservation , but how to get the reservation instance from outside, like reservation.close();

或者可以通过任何方式禁用Android 8.0中的热点

Or any way can disable hotspot in Android 8.0

[更新] ,我发现了一种可以禁用热点的方法

[Update] I found a way can disable hotspot

wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method method = wifiManager.getClass().getDeclaredMethod("cancelLocalOnlyHotspotRequest");
method.invoke(wifiManager);

但是,仍然不知道如何使用close.

But still, have no idea about how to use close.

推荐答案

要禁用它,您需要为WifiManager.LocalOnlyHotspotReservation创建一个全局引用,在onSatrted()回调中对其进行分配,然后按如下所示将其关闭

In order to disable it, you need to create a global reference for the WifiManager.LocalOnlyHotspotReservation, assign it in the onSatrted() callback and then close it as follows

private WifiManager.LocalOnlyHotspotReservation mReservation;

private void turnOffHotspot() {
 if (mReservation != null) {
  mReservation.close();
 }
}

您可以参考以下链接,它对我有用:如何打开/关闭Android 8.0(Oreo)中以编程方式创建wifi热点

You may refer to the following link as follows, it worked for me:How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo)

这篇关于如何在Android 8.0(Oreo)中以编程方式关闭wifi热点(setWifiApEnabled不再支持该版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 13:49