如何在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)