从另一个应用程序调用服务的权限

时间:2013-07-11 03:58:46

标签: android android-service android-permissions

可能重复:

我有两个应用程序说App" A"和App" B"。 App A有一项具有自定义权限的服务,而App B想要调用该服务。以下是我的代码段

应用A::清单文件

  <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>

我已经使用权限 android.permission.MyService

来保护我的服务

App B 清单文件具有以下权限

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

最后拨打App of Service A,我在App B中使用以下代码

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

当我运行此示例时,我正在使用以下堆栈跟踪获取SecurityException

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 

我已经完成了上面提到的许多线程,但是没有一个能够解决我的问题。所以,我再次提出了这个问题。

由于

1 个答案:

答案 0 :(得分:2)

我认为你错过了App manifest A中的权限声明。

http://developer.android.com/guide/topics/security/permissions.html 请参阅声明和强制执行权限。

此外,如果这两个应用程序都是由您签名和创作的(使用相同的签名),您可以在两个清单中声明它们,以便安装它们的顺序不会影响这一点。

理想情况下,权限名称应为.permission。