Not allowed to bind to service Intent

Viewed 43478

I write an android service of get weather, and the AndroidManifest.xml is:

<?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 let 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 the 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 solved this issue?

Other apk is use the Messenger method to communication with weather service.

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

Thanks all!

Now I modify my weather service's 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"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.my.weather.WeatherService"></action>
            </intent-filter>
        </service>
    </application>

</manifest>

And I use this method to start:

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

Then I got the warning message:

W/ActivityManager(131): Unable to start service Intent { act=com.my.weather.WeatherService }: not found
6 Answers

When you call bindService for a remote service, you should set your packageName too.

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

My problem was solved by David Wasser in a comment so I thought I share.

In the Manifest you can set exported attribute to true. This makes the service accessible to other apps.

Manifest.xml

<service android:name=".MyUsefulService_"
                android:exported="true"/>

MyActivity.java

Intent intent = new Intent();
intent.setComponent(new ComponentName("jav.android.app",jav.android.app.MyUsefulService_");

bindService(this,MyServiceConnection,0);
Related