Search code examples
androidandroid-intentandroid-serviceintentfilter

Android bind service with intent filter


I have two separate applications and ideally I would like to use an IntentFilter to bind to the service on the second application. Is this possible?

In app 1 Manifest:

    <service
        android:name=".ServiceName"
        android:exported="true"
        android:icon="@drawable/ic_launcher"
        android:label="Bound Service" >
        <intent-filter>
            <action android:name="IntentFilterName" />
        </intent-filter>
    </service>

In app 2 Activity:

It is currently :

@Override
protected void onResume() {
    super.onResume();
    Intent i = new Intent();
    i.setComponent(new ComponentName("com.test.application1", "com.test.application1.ServiceName")); 
    bindService(i, mConnection, Context.BIND_AUTO_CREATE);
}

Is it possible to work like this :

@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter1 = new IntentFilter("IntentFilterName"); 
    bindService(filter1, mConnection, Context.BIND_AUTO_CREATE);
}

I get the following error:

The method bindService(Intent, ServiceConnection, int) in the type ContextWrapper is not applicable for the arguments (IntentFilter, ServiceConnection, int)

Solution

  • You could call the IntentFilter through the intent as so:

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent("IntentFilterName"); 
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }