WifiManager.updateNetwork 在 Marshmallow 中不起作用
WifiManager.updateNetwork not working in Marshmallow
我一直在我的应用程序中使用 Wifimanager.updateNetwork(wifiConfiguration);
,它一直在 Kitkat、JellyBean 和 ICS 上工作,但现在在 Marshmallow 中它一直返回 -1,即使 wifiConfiguration 不为 null 并且具有有效的 networkId .有谁知道这可能的原因吗? Marshmallow 对此有什么改变吗?
根据Android 6.0 Wi-Fi and Networking Changes,
Your apps can now change the state of WifiConfiguration objects only if you created these objects. You are not permitted to modify or delete WifiConfiguration objects created by the user or by other apps.
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
if(Build.VERSION.SDK_INT < 23){
//your code here
}else {
requestContactPermission();
}
private void requestContactPermission() {
int hasContactPermission =ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_WIFI_STATE);
if(hasContactPermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(Context, new String[]{Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.ACCESS_WIFI_STATE}, PERMISSION_REQUEST_CODE);
}else {
//Toast.makeText(AddContactsActivity.this, "Wifi Permission is already granted", Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
// Check if the only required permission has been granted
if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("Permission", "Wifi permission has now been granted. Showing result.");
Toast.makeText(this," Wifi Permission is Granted",Toast.LENGTH_SHORT).show();
} else {
Log.i("Permission", "Wifi permission was NOT granted.");
}
break;
}
}
我一直在我的应用程序中使用 Wifimanager.updateNetwork(wifiConfiguration);
,它一直在 Kitkat、JellyBean 和 ICS 上工作,但现在在 Marshmallow 中它一直返回 -1,即使 wifiConfiguration 不为 null 并且具有有效的 networkId .有谁知道这可能的原因吗? Marshmallow 对此有什么改变吗?
根据Android 6.0 Wi-Fi and Networking Changes,
Your apps can now change the state of WifiConfiguration objects only if you created these objects. You are not permitted to modify or delete WifiConfiguration objects created by the user or by other apps.
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
if(Build.VERSION.SDK_INT < 23){
//your code here
}else {
requestContactPermission();
}
private void requestContactPermission() {
int hasContactPermission =ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_WIFI_STATE);
if(hasContactPermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(Context, new String[]{Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.ACCESS_WIFI_STATE}, PERMISSION_REQUEST_CODE);
}else {
//Toast.makeText(AddContactsActivity.this, "Wifi Permission is already granted", Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
// Check if the only required permission has been granted
if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("Permission", "Wifi permission has now been granted. Showing result.");
Toast.makeText(this," Wifi Permission is Granted",Toast.LENGTH_SHORT).show();
} else {
Log.i("Permission", "Wifi permission was NOT granted.");
}
break;
}
}