Permission in calling service from another app

2019-07-16 02:17发布

问题:

Possible Duplicates:

  • Permission for services,
  • Permission issue while starting a service from android,
  • Exported service does not require permission: what does it mean?,
  • Not allowed to start service Intent without permission etc

I have two applications say App "A" and App "B". App A has one service with custom permission and App B wants to call that service. Following is my code snippet

App A:: Manifest File

  <service
            android:name="SendService"
            android:permission="android.permission.MyService">
            <intent-filter>
                <action android:name="com.example.calledactivity.MyServiceCaller" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:scheme="sms" />
                <data android:scheme="smsto" />
            </intent-filter>
        </service>

Here i have secured my service using permission android.permission.MyService

in App B Manifest file have following permission

<uses-permission android:name="android.permission.MyService"
    android:description="@string/app_name"
    android:label="@string/menu_settings" />

And finally to call Service of App A, i am using following code in App B

Intent i = new Intent("com.example.calledactivity.MyServiceCaller", Uri.parse("sms:2223333"));
                    getApplicationContext().startService(i);

When i run this sample i am getting SecurityException with following stack trace

12-05 23:35:41.526: W/dalvikvm(25730): threadid=1: thread exiting with uncaught exception (group=0x40d3cac8)
12-05 23:35:41.526: W/ActivityManager(752): Permission Denial: Accessing service ComponentInfo{com.example.calledactivity/com.example.calledactivity.SendService} from pid=25730, uid=10159 requires android.permission.MyService
12-05 23:35:41.536: E/AndroidRuntime(25730): FATAL EXCEPTION: main
12-05 23:35:41.536: E/AndroidRuntime(25730): java.lang.SecurityException: Not allowed to start service Intent { act=com.example.calledactivity.MyServiceCaller dat=sms:xxxx } without permission android.permission.MyService
12-05 23:35:41.536: E/AndroidRuntime(25730):    at android.app.ContextImpl.startServiceAsUser(ContextImpl.java:1714)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at android.app.ContextImpl.startService(ContextImpl.java:1686)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at android.content.ContextWrapper.startService(ContextWrapper.java:457)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at com.example.callingactivity.MainActivity$1.onClick(MainActivity.java:29)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at android.view.View.performClick(View.java:4383)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at android.view.View$PerformClick.run(View.java:18097)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at android.os.Handler.handleCallback(Handler.java:725)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at android.os.Looper.loop(Looper.java:137)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at android.app.ActivityThread.main(ActivityThread.java:5279)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at java.lang.reflect.Method.invokeNative(Native Method)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at java.lang.reflect.Method.invoke(Method.java:511)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-05 23:35:41.536: E/AndroidRuntime(25730):    at dalvik.system.NativeStart.main(Native Method)
12-05 23:35:41.556: D/InputDispatcher(752): Focused application set to: AppWindowToken{4231fa58 token=Token{4231df58 ActivityRecord{4231dce8 u0 com.sec.android.app.launcher/com.android.launcher2.Launcher}}}
12-05 23:35:41.556: W/ActivityManager(752):   Force finishing activity com.example.callingactivity/.MainActivity
12-05 23:35:41.576: W/ContextImpl(752): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1379 com.android.server.am.ActivityStack.startPausingLocked:1408 com.android.server.am.ActivityStack.finishActivityLocked:5920 com.android.server.am.ActivityStack.finishActivityLocked:5834 com.android.server.am.ActivityManagerService.handleAppCrashLocked:9529 

I have already gone through many of the threads mentioned above, but none of them were able to solve my problem. So, i have raised this question again.

Thanks

回答1:

I think you're missing the permission declaration in App manifest A.

http://developer.android.com/guide/topics/security/permissions.html See Declaring and Enforcing permissions.

Also, if both the apps are signed and authored by you (with the same signature) you can declare them in both manifests so that the order in which they are installed doesn't affect this.

Ideally the permission name should be .permission.