解決跨應用程序啓動services報without a permission not allowed for APK的問題

appA去啓動appB的Sevices的情況:

(1)appB的Services需要配置:

<service
      android:name=".MyService"    
      android:exported="true"
      android:permission="app.my.custom.services.permission">
 
      <intent-filter>
           <action android:name="android.intent.action.START_CUSTOM_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_CUSTOM_SERVICE");
intent.setComponent(new ComponentName("com.dh.custom.services","com.dh.custom.services.MyService"));
startService(intent);

以上 new ComponentName("AppB的包名","AppB中Service的全路徑");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章