Android 6.0中的ConnectivityManager.requestNetwork [英] ConnectivityManager.requestNetwork in Android 6.0

查看:873
本文介绍了Android 6.0中的ConnectivityManager.requestNetwork的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ConnectivityManager.requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)

原因是强制应用程序在某些没有互联网连接的特定网络(这是本地硬件通信网络)中调用将来的请求.此时,系统正在通过3G/4G网络发送请求,并且从未到达所需的Wifi网络,因为该网络没有响应

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.

当我调用requestNetwork方法时,出现以下错误:

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.

我尝试调用新方法来请求Android 6.0中可用的权限:

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);

但是回调始终为PackageManager.PERMISSION_DENIED.

我将这两个权限都放在了AndroidManifest.xml中,但没有成功.

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

注意:Manifest.permission.WRITE_SETTINGS不在权限组中.

推荐答案

我不确定这是否是Google想要的,但以下是我所看到的行为:

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

CHANGE_NETWORK_STATE似乎总是被拒绝(如注释中所述,它是签名权限),但也似乎无关紧要.我的ConnectivityManager网络请求似乎仅由WRITE_SETTINGS控制-因此,如果您具有WRITE_SETTINGS,则不需要CHANGE_NETWORK_STATE.

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);

然后,我的ConnectivityManager网络请求变得很桃色.

And after that, my ConnectivityManager network requests were peachy.

要在调用ACTION_MANAGE_WRITE_SETTINGS活动之前检查是否已授予许可,此答案的解决方案是 设置.System.canWrite(Context)

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)

无法获得WRITE_SETTINGS权限

更新:从Android 6.0.1开始,在清单文件中请求时会自动授予CHANGE_NETWORK_STATE权限.以上WRITE_SETTINGS检查仅对于6.0才需要

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

这篇关于Android 6.0中的ConnectivityManager.requestNetwork的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆