Not allowed to contact the service

I am writing an android service to get the weather, and AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.weather" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <service android:name=".WeatherService" android:exported="true" > </service> </application> </manifest> 

Now I want another apk to start this service:

 Intent service = new Intent(); service.setClassName("com.my.weather", "com.my.weather.WeatherService"); context.bindService(service, weatherServiceConnection, Context.BIND_AUTO_CREATE); 

And I got an error message:

 E/AndroidRuntime(14068): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.test/com.my.test.MainActivity}: java.lang.SecurityException: Not allowed to bind to service Intent { cmp=com.my.weather/.WeatherService } 

How can I solve this problem?

Another apk uses the Messenger method to communicate with the meteorological service.

==================================================== ================================

Thanks everyone!

Now I am modifying my AndroidManifest.xml meteorological service:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.weather" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <service android:name=".WeatherService" android:exported="true" android:process=":remote" > <intent-filter> <action android:name="com.my.weather.WeatherService"></action> </intent-filter> </service> </application> </manifest> 

And I use this method to run:

 Intent service = new Intent("com.my.weather.WeatherService"); context.bindService(service, weatherServiceConnection, Context.BIND_AUTO_CREATE); 

Then I got a warning message:

 W/ActivityManager(131): Unable to start service Intent { act=com.my.weather.WeatherService }: not found 
+9
android service
source share
5 answers

Here it says from the documentation:

If we want this service to work in a remote process (instead of the standard one for its .apk), we can use the android:process in its manifest tag to indicate one:

 <service android:name=".app.MessengerService" android:process=":remote" /> 

Also note that the remote name chosen here is arbitrary, and you can use other names if you need additional processes. Prefix : Adds a name to the standard process name of your package. After that, clients can now contact the service and send messages to it. Please note that this allows customers to register and receive messages back.

Edit1 :
Secondly, if the service element (in the manifest) contains an action string, use it. For example, if your service is declared like this:

 <service android:name="com.sample.service.serviceClass" android:exported="true" android:label="@string/app_name" android:process=":remote"> <intent-filter><action android:name="com.sample.service.serviceClass"></action> </intent-filter> </service> 

Do this in the onCreate() method of your service:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent=new Intent("com.sample.service.serviceClass"); this.startService(intent); } 

I saw this in this question:
Unable to start service intent

Edit2 :
I saw your manifest again. It looks like your manifest does not have Main/Launcher Activity . In android 3.1 and later it does not provide access to the service. In fact for security reason all services,receivers,... that you declare in manifest,will not register unless your App run explicitly by user and this requires Main / Startup activity. Therefore, you need to add such activity to your application and be careful that it has already been completed.

+6
source share

I had the same problem. For me, the problem was that I first installed the application using the API, and then the application that provided the API. Removing / reinstalling the first application solved the problem.

Update. To avoid the problem , both applications must determine the resolution in their manifest.

+9
source share

Add an intent filter with an action to your WeatherService:

 <service android:name=".WeatherService" android:exported="true" > <intent-filter> <action android:name="this.is.my.custom.ACTION" /> </intent-filter> </service> 

Then, when you switch to bind with bindService () in another application, use this intent:

 new Intent("this.is.my.custom.ACTION") 
+2
source share

In the activity class ur ... suppose BothButton is the name of the Activity class, and CleverTexting is the name of the service class, and you want to call an action from the service, not from the service. If ur code works fine in more than 4.0 version for android than u, you can do it, there will be no problem.

 CleverTexting mService ; public void setService(CleverTexting listener) { // TODO Auto-generated method stub mService = listener; } 

and in ur Service class do ... where u want to trigger ur activity

 BothButton mBothButton; mBothButton = new BothButton(this); mBothButton.setService(this); 
0
source share

When you call bindService for a remote service, you must also specify your packageName.

 Intent intent = new Intent("com.my.weather.WeatherService"); intent.setPackage("com.my.weather"); bindService(intent, serConn, Context.BIND_AUTO_CREATE); 
0
source share

All Articles