29

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.

Deividi Cavarzan
  • 10,034
  • 13
  • 66
  • 80
  • 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. – shkschneider Aug 24 '15 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... – Deividi Cavarzan Aug 24 '15 at 15:25
  • 2
    `WRITE_SETTINGS` is handled via [a different mechanism](http://stackoverflow.com/a/32083622/115145). 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`. – CommonsWare Aug 24 '15 at 15:28
  • @CommonsWare `normal` and `signature` should be treated equally anyway, right? – shkschneider Aug 25 '15 at 12:22
  • 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). – CommonsWare Aug 25 '15 at 12:45
  • @CommonsWare Was talking about https://developer.android.com/preview/features/runtime-permissions.html#system-apps. But nevermind my comment, your comment is entirely true. – shkschneider Aug 25 '15 at 13:07
  • @CommonsWare thanks!!! I'll take a look in your previous answer. Indeed, my nexus 5 also treated as `signature`. – Deividi Cavarzan Aug 27 '15 at 17:50
  • Is there any bug raised for this? Seeing the same error for my app while connectivityManager.requestNetwork API – Rupali Oct 06 '15 at 08:47
  • 2
    @Rupali look at https://code.google.com/p/android-developer-preview/issues/detail?id=2993&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars http://stackoverflow.com/questions/32208863/some-permissions-protectionlevel-seems-wrongly-documented-or-implemented/32519310#32519310 Whats the status in official ROM? – NitZRobotKoder Oct 19 '15 at 07:07
  • @NitZRobotKoder No resolution until now. I'm using the target API 22 to make it work with previous permission model (in AndroidManifest.xml only) – Deividi Cavarzan Oct 31 '15 at 17:47

2 Answers2

18

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

fawaad
  • 341
  • 6
  • 12
Splash
  • 1,310
  • 12
  • 20
  • 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) http://stackoverflow.com/questions/32083410/android-m-6-0-ringtonemanager-manifest-permission-write-settings-error/32083622#32083622 – Deividi Cavarzan Nov 04 '15 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 09 '15 at 09:53
13

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.

cuihtlauac
  • 1,808
  • 2
  • 20
  • 39