在安卓系统中启动服务时出现权限问题

14

我编写了一个应用程序的服务。我尝试使用以下代码从第二个应用程序调用此服务:

    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);

它会引发以下异常

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
为什么会出现这个权限问题?
当我从同一个应用程序调用服务时,以下代码功能完美:
    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);

当我从一个不同的应用程序调用服务时,尽管我在App2的清单文件中已经授予了所有必需的权限,但为什么会出现权限问题呢?

编辑

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>

包含服务的App1清单文件

<?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>

似乎你的清单文件中缺少一些内容。请添加你两个应用程序的清单文件。 - Ran
你们两个应用程序的uID不同。 - user370305
2个回答

17

因为您没有明确地让服务可用,所以其他应用程序无法使用该服务。您的服务需要定义如下:

<service android:name="DownloadService"
         android:exported="true">
</service>

您不需要任何额外的权限(除非您想使用它们)


2
你读过文档了吗?它对这个问题解释得很清楚:
如果在服务的清单文件中声明了标签,那么可以强制全局访问该服务。这样一来,其他应用程序需要在其自己的清单文件中声明相应的元素,才能够启动、停止或绑定到该服务。
从GINGERBREAD版本开始,当使用Context.startService(Intent)时,您还可以在Intent上设置Intent.FLAG_GRANT_READ_URI_PERMISSION和/或Intent.FLAG_GRANT_WRITE_URI_PERMISSION。这将授予服务临时访问Intent中特定URI的权限。访问将保持到服务为该启动命令或以后的启动命令调用stopSelf(int)方法,或者直到服务被完全停止。这适用于授予未请求保护服务的权限的其他应用程序,甚至在服务根本没有被导出时也可以。
此外,服务可以通过在执行IPC调用实现之前调用checkCallingPermission(String)方法来使用权限保护各个IPC调用。
有关权限和安全性的更多信息,请参阅“安全性和权限”文档。
如果您在阅读文档后仍然有权限问题,请向我们展示您的AndroidManifest.xml文件。还请参考您清单文件中服务的exportedpermission属性。

我尝试添加 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);,但仍然无法解决问题并且会产生相同的异常。 - user1400538

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接