Problème d'autorisation lors du démarrage d'un service à partir d'Android
J'ai écrit un service dans le cadre d'une application. J'essaie de les appeler ce service à partir d'une deuxième application à l'aide du code suivant:
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);
Il donne l'exception suivante
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
Pourquoi cette autorisation problème se produit?
Le code suivant fonctionne parfaitement quand je l'ai appeler le service à partir de la même application:
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);
Quelqu'un peut s'il vous plaît laissez-moi savoir pourquoi je reçois ce problème d'autorisation quand j'ai appeler le service à partir d'une autre application, si je vous ai donné toutes les autorisations nécessaires dans App2 manifeste.
MODIFIER
Manifeste de 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>
Manifeste de App1 qui contient le 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>
source d'informationauteur user1400538
Vous devez vous connecter pour publier un commentaire.
Le service n'est pas disponible pour les autres applications, car vous n'avez pas fait explicitement disponibles. Votre service doit être défini comme ceci:
Vous n'avez pas besoin de droits supplémentaires (sauf si vous voulez les utiliser)
Votre lire la la documentation? Il est évident qu'avec cette question:
Nous montrer votre
AndroidManifest.xml
si vous avez encore des problèmes d'autorisation après la lecture de la documentation. Voir aussi laexporté
etpermission
de l'attribut de service dans votre manifeste.