Not allowed to start service Intent without permission - sender does not get permissions
Asked Answered
M

2

7

I'm working with Mark Murphy's excellent Commonsware books - but it's a lot to digest. I built the 'FakePlayer' app (pretends to be an mp3 player). It contains a service. As a learning experience I tried to write a trivial app (has only a button) whose click handler does:

Intent i = new Intent();
i.setAction("com.example.cwfakeplayer.MyPlayerService");
Context context = getApplicationContext();
context.startService(i);

It worked fine - the service start ok. I noticed Eclipse complaining about no permission on the service, so I updated the service's manifest by adding 2 lines, android:permissions and android:exported:

    <service 
        android:name="MyPlayerService"
        android:permission="com.example.fakeplayer.permission.MY_PLAYER_PERMISSION"
        android:exported="true"
       <intent-filter>
            <action android:name="com.example.fakeplayer.MyPlayerService"></action>
       </intent-filter>
      </service>

I reloaded the player app onto the device (I'm using a Galaxy S2) using 'debug' under eclipse. It seemed to work; the starter app caused a permission exception, which I expected.

I then added to the starter app's manifest (to give it the permission):

<manifest
  ...
  <uses-sdk ....
  ....
  <uses-permission android:name="com.example.fakeplayer.permission.MY_PLAYER_PERMISSION" />

I reloaded the starter app onto the device (using debug under Eclipse). Still get the permission error in the starter app.

I removed both apps from the device and reinstalled (using debug...), service app first, then starter. Still get perm error.

I am working my way through the 'how to use a remote service' section of Mr. Murphy's Advanced Android book, so I realized this is not the best way perhaps to work across apps.

I did a 'adb shell dumpsys package', located the starter app, and found it had 'permissionsFixed=false' and no 'grantedPermissions' section. I take this to mean the manifest change in the starter app did not manage to get the perm added to the app. But I have no idea why. As a learning experience, it's generated only confusion so far....

Any clues greatly appreciated! Thanks!

Maguire answered 18/3, 2013 at 1:42 Comment(0)
Z
5

I updated the service's manifest by adding 2 lines, android:permissions and android:exported

Technically, android:exported="true" is superfluous, as having the <intent-filter> automatically makes the <service> be exported.

I removed both apps from the device and reinstalled (using debug...), service app first, then starter. Still get perm error.

You do not show where you ever declare the custom permission with the <permission> element. In practice, if you control both apps, put the same <permission> element in both manifests, so the order of installation of your two apps no longer matters.

Zelda answered 18/3, 2013 at 11:17 Comment(6)
It also helps to include the <permission> in the first place. Thanks for the answer and for the excellent books and example code. I did not see any example code in the Android book that uses <permission> so I overlooked it in mine. (I did read the chapter - but I am very 'code focused').Maguire
@ArtSwri: Well, the book you apparently are reading ("Advanced Android") was retired a year ago, with its contents folded into my main book. That book definitely covers the <permission> element for custom permissions, as I use them in a plugin example: github.com/commonsguy/cw-omnibus/tree/master/RemoteViews That being said, thanks for the kind words!Zelda
Wow I'm further behind than I thought - my subscription ran out. Leaving now to renew...Maguire
@Zelda To the second half of your answer - isn't that what he did with the android:permission=... inside the <service> tag?Turnbuckle
@DavidDoria: The android:permission attribute does not declare a custom permission. <permission> does. android:permission states that a component should be defended by a permission (custom or otherwise), but if that permission does not exist, it will not be used.Zelda
after remove of "android:exported="true" error: As of Android 12, android:exported must be set; use true to make the activity available to other apps, and false otherwiseDevolution
H
-2

Try replace this in your manifest

<service android:name="com.example.fakeplayer.MyPlayerService"></service>

instead of

<service 
    android:name="MyPlayerService"
    android:permission="com.example.fakeplayer.permission.MY_PLAYER_PERMISSION"
    android:exported="true"
   <intent-filter>
        <action android:name="com.example.fakeplayer.MyPlayerService"></action>
   </intent-filter>
  </service>

If this doesn't work, kindly post out your error.

Hifalutin answered 18/3, 2013 at 3:8 Comment(1)
It's my understanding that removing the <intent> will cause exported to default to false, which means the service is not visible outside the app that contains it. So what I expected happened: the startService() in the starter app does nothing - there is nothing visible to it that will handle the Intent. (There is nothing in the log besides the Log.d I have in the button-clicked method. I do appreciate your effort, but don't think it's actually a solution.Maguire

© 2022 - 2024 — McMap. All rights reserved.