问题描述
我在 Samsung J200F 中测试了这些代码,效果最好,但是当我在 Lenovo 7.0 Android Nought 中测试了这些代码时, 他们不工作。 Logcat 中没有任何例外。 我已经在运行时和 AndroidManifest
中添加了这些权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
public class HotspotControl {
private static Method getWifiApConfiguration;
private static Method getWifiApState;
private static Method isWifiApEnabled;
private static Method setWifiApEnabled;
private static Method setWifiApConfiguration;
private WifiManager wm;
private String deviceName;
private WifiManager.LocalOnlyHotspotReservation mReservation;
private static HotspotControl instance = null;
static {
for (Method method : WifiManager.class.getDeclaredMethods()) {
switch (method.getName()) {
case "getWifiApConfiguration":
getWifiApConfiguration = method;
break;
case "getWifiApState":
getWifiApState = method;
break;
case "isWifiApEnabled":
isWifiApEnabled = method;
break;
case "setWifiApEnabled":
setWifiApEnabled = method;
break;
case "setWifiApConfiguration":
setWifiApConfiguration = method;
break;
}
}
}
private HotspotControl(Context context) {
wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
}
public boolean turnOnPreOreoHotspot(String name) {
wm.setWifiEnabled(false);
//Create new Open Wifi Configuration
WifiConfiguration wifiConf = new WifiConfiguration();
wifiConf.SSID = "\"" + name + "\"";
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wm.addNetwork(wifiConf);
wm.saveConfiguration();
return setHotspotEnabled(wifiConf,true);
}
private boolean setHotspotEnabled(WifiConfiguration config,boolean enabled) {
Object result = invokeSilently(setWifiApEnabled,wm,config,enabled);
if (result == null) {
return false;
}
return (Boolean) result;
}
private static Object invokeSilently(Method method,Object receiver,Object... args) {
try {
return method.invoke(receiver,args);
} catch (illegalaccessexception | IllegalArgumentException | InvocationTargetException e) {
Log.e(TAG,"exception in invoking methods: " + e.getMessage());
}
return null;
}
}
在这种情况下,您能帮我吗? 预先感谢。
解决方法
有一个完整的示例示例,该示例适用于所有版本。 它适用于Version = 23。 只需在 onCreate()上添加 checkLocationPermission()。
// Check for GPS permission
private boolean checkLocationPermission() {
if (Build.VERSION.SDK_INT >= 23 )
if(getContext().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED || getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || getContext().checkSelfPermission(Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED || getContext().checkSelfPermission(Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(
getActivity(),new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.CHANGE_WIFI_STATE},PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION
);
return false;
}
return true;
}
private boolean showWritePermissionSettings() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
if (!Settings.System.canWrite(getContext())) {
Log.v("DANG"," " + !Settings.System.canWrite(getContext()));
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getContext().getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
return false;
}
}
return true; //Permission already given
}
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
switch (requestCode) {
case PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (showWritePermissionSettings())
Logic();
} else {
// else type 1st
// dialog interface start 1st
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION)) {
// dialog
showOptionsDialogWithListners(getString(R.string.p2p_receiver_gps_permission_warning),new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
checkLocationPermission();
}
},int which) {
dialog.dismiss();
}
},"Re-Try","Yes,Im Sure");
// dialog interface end 1st
} // else type 2nd
else {
// dialog interface 2nd start
showOptionsDialogWithListners(getString(R.string.p2p_receiver_gps_no_permission_prompt),int which) {
try {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package",getContext().getPackageName(),null);
intent.setData(uri);
startActivity(intent);
} catch (ActivityNotFoundException anf) {
Toast.makeText(getContext(),"Settings activity not found",Toast.LENGTH_SHORT).show();
}
}
},int which) {
dialog.cancel();
}
},getString(R.string.label_settings),getString(R.string.Action_cancel));
// dialog interfaces 2nd ended
}
// else type 2nd start
}
// else 1st ended
break;
}
}
// Dialog with interfaces
public void showOptionsDialogWithListners(String message,DialogInterface.OnClickListener pListner,DialogInterface.OnClickListener nListener,String pButtonText,String nButtonText) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.AppCompatAlertDialogStyle);
builder.setCancelable(false);
builder.setMessage(Html.fromHtml(message));
builder.setPositiveButton(pButtonText,pListner);
builder.setNegativeButton(nButtonText,null != nListener ? nListener
: new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
return;
}
});
builder.show();
}