14

アプリケーションの一部としてサービスを作成しました。次のコードを使用して、2 番目のアプリケーションからこのサービスを呼び出そうとします。

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

2 に答える 2

17

サービスを明示的に利用可能にしていないため、他のアプリケーションはサービスを利用できません。サービスは次のように定義する必要があります。

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

追加のアクセス許可は必要ありません (使用したくない場合)。

于 2012-07-11T13:26:50.037 に答える
2

ドキュメントを読みましたか?この問題では非常に明確です。

マニフェストのタグでサービスが宣言されている場合、サービスへのグローバル アクセスを適用できます。そうすることで、他のアプリケーションは、独自のマニフェストで対応する要素を宣言して、サービスを開始、停止、またはバインドできるようにする必要があります。

GINGERBREAD の時点で、Context.startService(Intent) を使用する場合、Intent.FLAG_GRANT_READ_URI_PERMISSION および/または Intent.FLAG_GRANT_WRITE_URI_PERMISSION を Intent に設定することもできます。これにより、インテント内の特定の URI への一時的なアクセスがサービスに許可されます。サービスがその開始コマンドまたはそれ以降の開始コマンドに対して stopSelf(int) を呼び出すまで、またはサービスが完全に停止されるまで、アクセスは維持されます。これは、サービスを保護する許可を要求していない他のアプリへのアクセスを許可する場合や、サービスがまったくエクスポートされていない場合でも機能します。

さらに、サービスは、その呼び出しの実装を実行する前に checkCallingPermission(String) メソッドを呼び出すことにより、個々の IPC 呼び出しをアクセス許可で保護できます。

アクセス許可とセキュリティ全般の詳細については、セキュリティとアクセス許可のドキュメントを参照してください。

AndroidManifest.xmlドキュメントを読んでも権限の問題が解決しない場合は、その旨をお知らせください。マニフェスト内のサービスのexportedおよびpermission属性も参照してください。

于 2012-07-10T09:54:54.967 に答える