Not allowed to start service Intent without permis

2019-06-27 01:08发布

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!

2条回答
Viruses.
2楼-- · 2019-06-27 01:46

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.

查看更多
叼着烟拽天下
3楼-- · 2019-06-27 02:00

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.

查看更多
登录 后发表回答