30

I'm trying to get the new ConnectivityManager.bindProcessToNetwork(Network) using ConnectivityManager.requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)

The reason is to force the app to call the future request in some specific network, which doesn't have a internet connectivity (it's a local hardware communication network). At this point, the system is sending the requests over 3G/4G network and never reach the desired Wifi network, because this network doesn't respond the connectivity check that android call.

When I call the requestNetwork method, I receive the following error:

java.lang.SecurityException: com.xyz.app was not granted  either of these permissions: android.permission.CHANGE_NETWORK_STATE, android.permission.WRITE_SETTINGS.

I try to call the new method to request permission available in Android 6.0:

 requestPermissions(new String[]{Manifest.permission.CHANGE_NETWORK_STATE, Manifest.permission.WRITE_SETTINGS}, PERMISSIONS_REQUEST_WIFI);

But the callback is always PackageManager.PERMISSION_DENIED.

I put both of these permissions in the AndroidManifest.xml, without success.

Notice: The Manifest.permission.WRITE_SETTINGS is not in the Permissions Groups.

10
  • Indeed non of those 2 permissions are tagged "dangerous". No idea why it won't work. But you'r link to "connectivity check" code seems broken, so I can't have a look at it. Aug 24, 2015 at 15:22
  • @shkschneider actually I saw the link in the logcat for each network connected. It's a simple blank page this 204 http result. maybe this can change dinamically... Aug 24, 2015 at 15:25
  • 2
    WRITE_SETTINGS is handled via a different mechanism. In terms of CHANGE_NETWORK_STATE, I am not sure what the story is there -- the docs claim it is normal, but my 6.0-equipped Nexus 5 says it is signature. Aug 24, 2015 at 15:28
  • 1
    @shkschneider: Sorry, but I do not know what you mean by that. You can't employ a signature permission unless you are signed by the same signing key as whatever is defending itself with that permission, or unless there is some other mechanism (e.g., how WRITE_SETTINGS and SYSTEM_ALERT_WINDOW are handled now). Aug 25, 2015 at 12:45
  • 2
    @Rupali look at code.google.com/p/android-developer-preview/issues/… stackoverflow.com/questions/32208863/… Whats the status in official ROM? Oct 19, 2015 at 7:07

2 Answers 2

19

I'm not sure if this was intended by Google, but the following is the behavior I'm seeing:

CHANGE_NETWORK_STATE seems to always be denied (as noted in the comments, its a signature permission) but it also doesn't seem to matter. My ConnectivityManager network requests all seem to be gated by WRITE_SETTINGS only - so if you have WRITE_SETTINGS you don't need CHANGE_NETWORK_STATE.

As noted in comments, you do this differently than other permissions, using:

 Intent goToSettings = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
 goToSettings.setData(Uri.parse("package:" + Context.getPackageName()));
 startActivity(goToSettings);

And after that, my ConnectivityManager network requests were peachy.


To check if the permission is already granted before calling the ACTION_MANAGE_WRITE_SETTINGS activity, this answer has the solution using Settings.System.canWrite(Context)

Can't get WRITE_SETTINGS permission


UPDATE: as of Android 6.0.1, CHANGE_NETWORK_STATE is auto granted when requested in your manifest file. The above WRITE_SETTINGS checks are only required for 6.0

2
  • 1
    It works, thanks for your answer! But I notice that even the user accept the WRITE_SETTINGS permission manually, next time I launch the same activity the result of the permissions still marked as PERMISSION_DENIED. I need to apply this solution before calling the Intent again (avoid go to settings when it's already enabled) stackoverflow.com/questions/32083410/… Nov 4, 2015 at 13:16
  • This is no longer needed in Android 6.0.1. WRITE_SETTING and CHANGE_NETWORK_STATE are no longer the same thing.
    – cuihtlauac
    Dec 9, 2015 at 9:53
14

This was an Android 6.0 bug. It's fixed in Android 6.0.1, requestNetwork() can be called if you request CHANGE_NETWORK_STATE in the manifest. No need to call requestPermissions(), it's a normal permission.

1
  • 1
    Nice, this sounds really good! I'll take a look in my sources and upgrade to 6.0.1 and check it! Thanks! Dec 9, 2015 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.