简体   繁体   中英

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. 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.

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.

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 . 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. The only communication with it is through the Service API (binding and starting).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM