我引用代码打开Android 8.0中的热点,这是有用的。但我不知道如何禁用它
@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();
或以任何方式禁用Android 8.0中的热点
[更新] 我找到了一种方法可以禁用热点
wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method method = wifiManager.getClass().getDeclaredMethod("cancelLocalOnlyHotspotRequest");
method.invoke(wifiManager);
但仍然不知道如何使用close。
答案 0 :(得分:0)
要禁用它,您需要为WifiManager.LocalOnlyHotspotReservation创建一个全局引用,在onSatrted()回调中分配它,然后按如下所示关闭它
private WifiManager.LocalOnlyHotspotReservation mReservation;
private void turnOffHotspot() {
if (mReservation != null) {
mReservation.close();
}
}
您可以参考以下链接,它对我有用: How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo)