且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SecurityException异常:权限被拒绝(缺少INTERNET权限?)

更新时间:2022-02-19 21:30:24

[请务必阅读全部答案,包括。增编在最后!]

[Make sure you read whole answer, incl. addendum at the very end!]

异常你得到( SecurityException异常:权限被拒绝(缺INTERNET权限)),清楚地表明,你不能做网络。这是pretty的不争的事实。但如何能这样呢?通常它要么由于缺少<使用-权限的Andr​​oid:名称=android.permission.INTERNET对/> AndroidManifest.xml中的条目文件,或如Internet权限被授予在安装时不会在运行时,通过长期,错过了错误,在Android的框架,使您的应用程序被成功安装,但没有预期的权限授予。

Exception you are getting (SecurityException: Permission denied (missing INTERNET permission?)), clearly indicates that you are not allowed to do networking. That's pretty indisputable fact. But how can this happen? Usually it's either due to missing <uses-permission android:name="android.permission.INTERNET" /> entry in your AndroidManifest.xml file or, as internet permission is granted at installation not at run time, by long standing, missed bug in Android framework that causes your app to be successfully installed, but without expected permission grant.

我的清单是正确的,那么怎么能这样呢?

从理论上讲,的presence使用,许可的清单完全满足要求,从开发商的角度来看是所有的事情要做,能够做到网络。此外,由于权限都显示在安装过程中的用户,您的应用程序收盘装机用户的设备上的事实意味着他/她给予你的要求的(否则安装将被取消),因此假设,如果执行你的code,则所有被授予请求的权限是有效的。一旦批准,用户不能撤销许可的其他方式比完全卸载该应用程序作为标准Android框架(从AOSP)没有提供这样的功能的时刻。

Theoretically, presence of uses-permission in Manifest perfectly fulfills the requirement and from developer standpoint is all that's needed to be done to be able to do networking. Moreover, since permissions are shown to the user during installation, the fact your app ended installed on user's device means s/he granted what you asked for (otherwise installation is cancelled), so assumption that if your code is executed then all requested permissions are granted is valid. And once granted, user cannot revoke the permission other way than uninstalling the app completely as standard Android framework (from AOSP) offers no such feature at the moment.

但事情正变得越来越棘手,如果你也不介意扎根的设备上运行你的应用程序了。这儿有工具,谷歌播放提供您的用户可以安装来控制安装的应用程序授予的权限在运行时 - 例如:的Permissions拒绝等。这也可以用的CyanogenMod ,供应商品牌(如LG的)或者other定制ROM ,拥有各种类型的隐私管理器或类似工具。

But things are getting more tricky if you also do not mind your app running on rooted devices too. There're tools available in Google Play your users can install to control permission granted to installed apps at run-time - for example: Permissions Denied and others. This can also be done with CyanogenMod, vendor brand (i.e. LG's) or other custom ROM, featuring various type of "privacy managers" or similar tools.

因此​​,如果应用程序被阻止无论哪种方式,它基本上由用户有意,如果是阻塞,实在是多个用户的问题在这种情况下(或他/她有什么不明白的某些选项/工具真的,什么是其后果)比你的,因为标准的SDK(和大多数应用程序都写有注意,SDK)根本不行为的方式。所以,我的强烈怀疑发生在股价的标准,无根设备(或供应商如三星,HTC,索尼等)的ROM这个问题。

So if app is blocked either way, it's basically blocked intentionally by the user and if so, it is really more user problem in this case (or s/he do not understand what certain options/tools really do and what would be the consequences) than yours, because standard SDK (and most apps are written with that SDK in mind) simply does not behave that way. So I strongly doubt this problem occurs on "standard", non-rooted device with stock (or vendor like Samsung, HTC, Sony etc) ROM.

我不想崩溃...

正确实施许可管理/闭锁机制必须面对一个事实,即应用程序可能不希望某些功能被授予,但不可用,并且需要处理,使所有的事情工作,可能导致不知情的应用程序最侵入的方式坠毁。例如,当某些功能被授予(即GPS,上网),可以提供从应用/用户的角度出发,但没有提供真实数据(即GPS可以随时返回没有COORDS(就像当你是室内),网络连接理所当然的,肯定的,但你不能做任何连接,因为没有路由)。这种情况下,应该由应用程序来处理已经因为这根本就在正常的日常使用会发生,在这种情况下任何崩溃应该是相当考虑应用问题。

Properly implemented permission management/blocking mechanism must deal with the fact that apps may not expect certain features to be granted but not available, and need to deal with and make all things work in most "invasive" way that could lead unaware apps to crash. For example, when certain feature is granted (i.e. GPS, Internet access) it can be made available from the app/user perspective but provide no real data (i.e. GPS can always return no coords (like when you are indoor), internet access is granted, sure, but you can not make any connection as there's no routing). Such cases should be handled by app already as this simply can happen during normal day usage and any crash in such situation should be rather considered application bug.

我们没有关于发生这种问题的诊断问题的W / O猜测环境的信息太多了,而是作为一种解决方案,您可以考虑使用setDefaultUncaughtExceptionHandler()赶上这样的意外的异常在未来和IE只需出示您的应用程序需要的只是崩溃的是什么权限,而不是用户的详细信息。请注意,使用这将与像Crittercism,ACRA和其他工具,最有可能发生冲突,所以如果你使用这些小心。

We lack too much information about the environment on which this problem occurs to diagnose problem w/o guessing, but as kind of solution, you may consider using setDefaultUncaughtExceptionHandler() to catch such unexpected exceptions in future and i.e. simply show user detailed information what permission your app needs instead of just crashing. Please note that using this will most likely conflict with tools like Crittercism, ACRA and others, so be careful if you use any of these.

备注

请注意, android.permission.INTERNET对是不是唯一的联网相关的,你可能需要在清单申报的尝试成功地做到网络的权限。有网​​络授予干脆权限允许应用程序打开网络套接字(这基本上是最基本的要求做任何网络数据传输)。但是,如果你的网络协议栈/库想获取有关网络信息,以及,那么你也将需要 android.permission.ACCESS_NETWORK_STATE 在你的清单(这也就是要求 HttpURLConnection的客户端(的看教程)。

Please be aware that android.permission.INTERNET is not the only networking related permission you may need to declare in manifest in attempt to successfully do networking. Having INTERNET permission granted simply allows applications to open network sockets (which is basically fundamental requirement to do any network data transfer). But in case your network stack/library would like to get information about networks as well, then you will also need android.permission.ACCESS_NETWORK_STATE in your Manifest (which is i.e. required by HttpUrlConnection client (see tutorial).

补遗@ 2015年7月16日

请注意,即将到来的Andr​​oid版本(目前名为 M )会带来运行权限的权限给予用户更多的控制权:

Please note that forthcoming Android version (currently named M) will bring Runtime Permissions giving user more control on the permissions:

这[...]介绍了一种新的权限模型,用户现在可以   在运行时直接管理应用程序的权限。这种模式使用户   提高可视性和控制权限,同时简化   安装和自动更新过程的应用程序开发人员。用户   可以授予或单独撤销权限安装的应用程序。

This [...] introduces a new permissions model, where users can now directly manage app permissions at runtime. This model gives users improved visibility and control over permissions, while streamlining the installation and auto-update processes for app developers. Users can grant or revoke permissions individually for installed apps.

请参阅行为细节的变化说明页面,并确保您的应用程序会正常运行的较新的系统呢!

See behavior changes description page for details and make sure your app will behave correctly on newer systems too!