同时开始从Android的服务权限问题 [英] Permission issue while starting a service from android
问题描述
我写了一个服务作为应用程序的一部分。我尝试使用以下code调用,从第二次申请该服务:
意向意图=新的意图();
intent.setClassName(com.test,com.test.DownloadService);
//为通信创建一个新的斜挎背
信使送信=新使者(处理);
intent.putExtra(信使信使);
intent.setData(Uri.parse(http://www.vogella.com/index.html));
intent.putExtra(urlpath,http://www.vogella.com/index.html);
this.startService(意向);
它提供了以下异常
9月7号至10日:27:28.819:ERROR / AndroidRuntime(4226):致命异常:主要
java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.inject / com.inject.MyActivity}:java.lang.SecurityException异常:不允许启动服务意向{DAT = HTTP://www.vogella.com/index。 HTML CMP = com.test / .DownloadService(有临时演员)}未经允许不能从UID 10109出口
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1971)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1996)
在android.app.ActivityThread.access $ 700(ActivityThread.java:126)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1156)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4458)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
在dalvik.system.NativeStart.main(本机方法)
java.lang.SecurityException异常:产生的原因不允许启动服务意向{DAT = HTTP://www.vogella.com/index.html CMP = com.test / .DownloadService(有临时演员)}未经允许不能从UID出口10109
在android.app.ContextImpl.startService(ContextImpl.java:1122)
在android.content.ContextWrapper.startService(ContextWrapper.java:359)
在com.inject.MyActivity.onCreate(MyActivity.java:28)
在android.app.Activity.performCreate(Activity.java:4465)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1935)
... 11更多
为什么会出现这种权限问题?
下面code完美的作品时,我打电话从同一个应用程序的服务:
意向意图=新的意图(这一点,DownloadService.class);
//为通信创建一个新的斜挎背
信使送信=新使者(处理);
intent.putExtra(信使信使);
intent.setData(Uri.parse(http://www.vogella.com/index.html));
intent.putExtra(urlpath,http://www.vogella.com/index.html);
startService(意向);
有人可以让我知道为什么我得到这个许可问题,当我拨打服务从不同的应用程序,但我已经给了所有必需的权限App2的清单。
修改
App2的的清单
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.inject
安卓版code =1
机器人:VERSIONNAME =1.0>
<使用-SDK安卓的minSdkVersion =14/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<应用机器人:标签=@字符串/ APP_NAME机器人:图标=@可绘制/ ic_launcher>
<活动机器人:名称=MyActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
< /舱单>
程序App1的清单,其中包含了服务
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.test
安卓版code =1
机器人:VERSIONNAME =1.0>
<使用-SDK安卓的minSdkVersion =14/>
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<活动
机器人:名称=。MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
&所述; /意图滤光器>
< /活性GT;
<服务机器人:名称=下载服务>
< /服务>
< /用途>
< /舱单>
因为你没有做它明确地提供的服务不提供给其他应用程序。您的服务需要这样定义的:
<服务机器人:名称=下载服务
机器人:出口=真正的>
< /服务>
您不需要任何额外的权限(除非你想使用它们)
I have written a service as part of an application. I try to call this service from a second application using the following code:
Intent intent = new Intent () ;
intent.setClassName("com.test" ,"com.test.DownloadService") ;
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(handler);
intent.putExtra("MESSENGER", messenger);
intent.setData(Uri.parse("http://www.vogella.com/index.html"));
intent.putExtra("urlpath", "http://www.vogella.com/index.html");
this.startService(intent);
It gives the following exception
07-10 09:27:28.819: ERROR/AndroidRuntime(4226): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.inject/com.inject.MyActivity}: java.lang.SecurityException: Not allowed to start service Intent { dat=http://www.vogella.com/index.html cmp=com.test/.DownloadService (has extras) } without permission not exported from uid 10109
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1971)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1996)
at android.app.ActivityThread.access$700(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1156)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4458)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Not allowed to start service Intent { dat=http://www.vogella.com/index.html cmp=com.test/.DownloadService (has extras) } without permission not exported from uid 10109
at android.app.ContextImpl.startService(ContextImpl.java:1122)
at android.content.ContextWrapper.startService(ContextWrapper.java:359)
at com.inject.MyActivity.onCreate(MyActivity.java:28)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1935)
... 11 more
Why this permission issue occurs?
The following code works perfect when I call the service from the same app:
Intent intent = new Intent(this, DownloadService.class);
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(handler);
intent.putExtra("MESSENGER", messenger);
intent.setData(Uri.parse("http://www.vogella.com/index.html"));
intent.putExtra("urlpath", "http://www.vogella.com/index.html");
startService(intent);
Can someone please let me know why I get this permission issue when I call the service from a different application, though I have given all the required permissions in App2's manifest.
EDIT
Manifest of App2
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inject"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Manifest of App1 which contains the service
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service android:name="DownloadService" >
</service>
</application>
</manifest>
The service isn't available to other applications because you haven't made it explicitly available. Your service needs to be defined like this:
<service android:name="DownloadService"
android:exported="true">
</service>
You don't need any extra permissions (unless you want to use them)
这篇关于同时开始从Android的服务权限问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!