3

可能的重复:

我有两个应用程序说应用程序“A”和应用程序“B”。应用 A 有一项具有自定义权限的服务,而应用 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 Manifest 文件中具有以下权限

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

最后调用应用程序 A 的服务,我在应用程序 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 

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

谢谢

4

1 回答 1

3

我认为您缺少应用清单 A 中的权限声明。

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

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

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

于 2013-07-11T04:29:29.887 回答