最近从 AppA 启动 AppB 的一个 service ,报了这个错误Not allowed to start service Intent
解决方案:
1.AppB中Service的配置
<service
android:name=".MyService"
android:exported="true"
android:permission="app.custom.permission">
<intent-filter>
<action android:name="android.intent.action.START_SERVICE"/>
</intent-filter>
</service>
name、exported、permission这三个属性是必不可少的 另外添加 intent-filter 里面的 action 是自定义的
2.AppB中Mainfest.xml的权限配置
<permission android:name="app.custom.permission"
android:protectionLevel="signature"/>
<uses-permission android:name="app.custom.permission"/>
3.AppA中Mainfest.xml的权限配置
<uses-permission android:name="app.custom.permission"/>
启动service的代码
Intent intent = new Intent();
intent.setAction("android.intent.action.START_SERVICE");
intent.setComponent(new ComponentName("com.aa.securityassitant","com.aa.securityassitant.MyService"));
startService(intent);
以上 new ComponentName("AppB的包名","AppB中Service的全路径");