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

[亡魂溺海] 提交于 2019-12-12 11:45:09

问题


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!


回答1:


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.




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



来源:https://stackoverflow.com/questions/15468567/not-allowed-to-start-service-intent-without-permission-sender-does-not-get-per

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!