android – 不允许未经许可启动服务Intent – 发件人无法获得权限

我正在使用Mark Murphy出色的Commonsware书籍 – 但要消化它还有很多东西.我建立了’FakePlayer’应用程序(假装是一个MP3播放器).它包含一项服务.作为一种学习经历,我尝试编写一个简单的应用程序(只有一个按钮),其单击处理程序执行:

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

它工作正常 – 服务开始正常.我注意到Eclipse抱怨该服务没有权限,所以我通过添加2行更新了服务的清单,android:permissions和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>

我在eclipse下使用’debug’将播放器app重新加载到设备上(我正在使用Galaxy S2).它似乎工作;启动应用程序导致了一个权限异常,这是我所期望的.

然后我添加到入门应用程序的清单(授予它权限):

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

我将入门应用程序重新加载到设备上(使用Eclipse下的调试).仍然在启动器应用程序中获得权限错误.

我从设备中删除了这两个应用程序并重新安装(使用调试…),首先是服务应用程序,然后是启动程序.仍然得到烫发错误.

我正在通过Murphy先生的高级Android书籍的“如何使用远程服务”部分工作,所以我意识到这不是跨应用程序工作的最佳方式.

我做了一个’adb shell dumpsys package’,位于启动应用程序中,发现它有’permissionsFixed = false’而没有’grantedPermissions’部分.我认为这意味着入门应用程序中的明显更改无法将perm添加到应用程序中.但我不明白为什么.作为一种学习经历,到目前为止它只产生了混乱….

任何线索非常感谢!谢谢!

最佳答案

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

从技术上讲,android:exported =“true”是多余的,因为它具有< intent-filter>自动生成< service>出口.

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

您没有使用< permission>来显示您声明自定义权限的位置元件.实际上,如果您控制这两个应用,请使用相同的 两个清单中的元素,因此两个应用程序的安装顺序不再重要.

点赞