我有一个第一个应用程序,其服务仅带有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
,则可以成功看到它,就像看到吐司一样。
我使用minSdkVersion=19
和compileSdkVersion=28
进行构建
我想念什么?
答案 0 :(得分:0)
您似乎在第一个应用程序的清单中没有权限声明
<permission android:name="com.example.myservice.permission.ACCESS_API"
android:protectionLevel="normal"/>
答案 1 :(得分:0)
尝试在第一个应用程序中将android:process=":my_process"
添加到您的服务标签中。
service elements