这是我的代码切换代码。
switchWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
turnOnOffWifi(BuddeeCityInsiderActivity.this, false);
setWifiTetheringEnabled(false);
if (isChecked) {
turnOnOffHotspot(BuddeeCityInsiderActivity.this, true);
} else {
turnOnOffHotspot(BuddeeCityInsiderActivity.this, false);
}
}
});
但是我要接受onCreate()
的许可像:
private void getPermission() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
EasyPermissions.requestPermissions(
BuddeeCityInsiderActivity.this,
getString(R.string.contact),
HOTSPOT_PHONE,
android.Manifest.permission.ACCESS_WIFI_STATE,
android.Manifest.permission.CHANGE_WIFI_STATE,
android.Manifest.permission.CHANGE_NETWORK_STATE,
android.Manifest.permission.WRITE_SETTINGS
);
} else {
}
}
on Clicking switchButton使用相应的参数调用此方法:
private void setWifiTetheringEnabled(boolean enable) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
Method[] methods = wifiManager.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("setWifiApEnabled")) {
try {
method.invoke(wifiManager, null, enable);
} catch (Exception ex) {
}
break;
}
}
}
在Android L中工作正常但在Android M或M +中出现问题请给我一些解决方案