首页 > 解决方案 > 在我的服务许可中我错过了什么

问题描述

我有一个只有包含 toast 的方法的服务的第一个应用程序。onBind()它是这样声明的:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myservice">
    <application
         ...>
        ....
        <service
            android:name=".ServiceApi"
            android:enabled="true"
            android:exported="true"
            android:permission="com.example.myservice.permission.ACCESS_API"/>
    </application>

我有第二个应用程序尝试绑定服务。我在清单中声明了访问权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myclient">
    ....
    <uses-permission android:name="com.example.myservice.permission.ACCESS_API" />

这是代码:

    val componentName = ComponentName("com.example.myservice", "com.example.myservice.ServiceApi")
    val intent = Intent()
    intent.component = componentName
    bindService(intent, mConnection,
            Context.BIND_AUTO_CREATE)

我收到以下错误:

07-14 09:35:13.786 31042-31042/com.example.myclient E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myclient, PID: 31042
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myclient/com.example.myclient.MainActivity}: java.lang.SecurityException: Not allowed to bind to service Intent { cmp=com.example.myservice/.ServiceApi }
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.SecurityException: Not allowed to bind to service Intent { cmp=com.example.myservice/.ServiceApi }
        at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1322)
        at android.app.ContextImpl.bindService(ContextImpl.java:1286)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:604)
        at com.example.myclient.MainActivity.onStart(MainActivity.kt:67)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
        at android.app.Activity.performStart(Activity.java:6268)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

如果我删除android:permission服务应用程序清单中的 ,我可以成功绑定它,因为我可以看到 toast。

我用什么构建,minSdkVersion=19我想念什么?compileSdkVersion=28

标签: androidserviceandroid-permissions

解决方案


您似乎在第一个应用程序的清单中没有权限声明

 <permission android:name="com.example.myservice.permission.ACCESS_API" 
       android:protectionLevel="normal"/>

推荐阅读