简体   繁体   English

Android服务异常-权限被拒绝缺少INTERNET权限

[英]Android Service Exception - Permission denied missing INTERNET permission

I have stumbled upon a problem I just can't seem to fix. 我偶然发现了一个我似乎无法解决的问题。

2 scenarios, in the first a user clicks a button which fires a method in MainActivity to make a web request. 在两种情况下,首先,用户单击一个按钮,该按钮会触发MainActivity的方法来发出Web请求。 This works, no problem at all. 这行得通,完全没有问题。

In the second scenario I want to make this request automatically in a service running in the background, as soon as the code moves into the Service, it makes a permission exception. 在第二种情况下,我想在后台运行的服务中自动发出此请求,一旦代码移入服务,它就会产生权限异常。

Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)

I can start up the service perfectly fine, but if I move any network requests into the service then I get into permission problems. 我可以很好地启动该服务,但是如果将任何网络请求移入该服务,则会遇到权限问题。

I already have <uses-permission android:name="android.permission.INTERNET"/> and <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> in my AndroidManifest.xml file. 我的AndroidManifest.xml文件中已经有<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

I also do realtime permission checking (SDK 23+) in my MainActivity before starting the service, but this seems to have no effect on the permissions that the service gets. 在启动服务之前,我还在MainActivity中进行了实时权限检查(SDK 23+),但这似乎对服务获得的权限没有影响。

Any ideas? 有任何想法吗?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="domain">

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name="domain.RemoteCommunicationService"
        android:isolatedProcess="true"
        android:exported="true">
    </service>
    <receiver android:name="domain.RemoteServiceRestartReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="domain.RemoteServiceRestart"></action>
        </intent-filter>
    </receiver>
    <receiver android:name="domain.RemoteCommunicationService$RemoteCommunicationReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="domain.RemoteCommunication.ToggleLight"></action>
        </intent-filter>
    </receiver>
</application>

You have use isolateProcess to true . 您已经使用isolateProcesstrue Read what docs has to say: 阅读文档怎么说:

If set to true, this service will run under a special process that is isolated from the rest of the system and has no permissions of its own. 如果设置为true,则此服务将在与系统其余部分隔离的特殊进程下运行,并且没有自己的权限。 The only communication with it is through the Service API (binding and starting). 与它的唯一通信是通过Service API(绑定和启动)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 权限被拒绝(缺少Internet权限?)-Android错误 - Permission denied (missing INTERNET permission?) - android error 权限被拒绝(缺少 INTERNET 权限) - Permission denied (missing INTERNET permission) 权限被拒绝 - 缺少INTERNET权限? - Permission denied - missing INTERNET permission? 权限被拒绝(缺少INTERNET权限?):但是获得了许可 - Permission denied (missing INTERNET permission?): But permission is given SecurityException:权限被拒绝(缺少 INTERNET 权限?) - SecurityException: Permission denied (missing INTERNET permission?) AsyncTask权限被拒绝(缺少INTERNET权限?) - AsyncTask Permission denied (missing INTERNET permission?) 权限被拒绝(缺少INTERNET权限?)未出现在logcat中 - Permission denied (missing INTERNET permission?) Not Appearing in logcat 缺少Android Internet权限 - Android internet permission missing java.lang.SecurityException:权限被拒绝(缺少INTERNET权限?),即使在android清单中添加了此权限 - java.lang.SecurityException: Permission denied (missing INTERNET permission?) even with adding this permission in android manifest NetworkDispatcher.run:未处理的异常java.lang.SecurityException:权限被拒绝(缺少INTERNET权限?) - NetworkDispatcher.run: Unhandled exception java.lang.SecurityException: Permission denied (missing INTERNET permission?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM