android - 在我的服务许可中我错过了什么
问题描述
我有一个只有包含 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
解决方案
您似乎在第一个应用程序的清单中没有权限声明
<permission android:name="com.example.myservice.permission.ACCESS_API"
android:protectionLevel="normal"/>
推荐阅读
- python - ValueError:操作数无法与形状一起广播 (780,12964) (19419,)
- sql - SQL Server 查询选择循环列
- python - 是 tcmalloc: large alloc Python 中的警告或错误
- mysql - 如何从 Tableau 调用 MySQL 存储过程
- python - Read_Excel Pandas Sheet_Name = None 仅返回 1 张
- json - 如何从嵌套字典中制作 DataFrame?
- ios - 更改文本视图键盘外观时应用程序崩溃
- python - python 3 - UnicodeEncodeError:'charmap'编解码器无法编码字符(编码使其在文件中)
- javascript - StencilJS - TypeScript - 导出枚举时“找不到名称......”
- javascript - Twitter bot 自动回复指定的主题标签