0

In App A, I have the following code to start a service in App B:

public void startAppBService() {
    PackageManager packageManager = getPackageManager();
    Intent serviceIntent = new Intent("com.example.project.appBCommunicator");
    List<ResolveInfo> services = packageManager.queryIntentServices(serviceIntent, 0);
    Log.d("Services", Integer.toString(services.size()));
    if (services.size() > 0) {
        ResolveInfo service = services.get(0);

        serviceIntent.setClassName(service.serviceInfo.packageName, service.serviceInfo.name);
        serviceIntent.setAction("com.example.project.appBReceive");
        serviceIntent.putExtra("foo", "bar");
        serviceIntent.putExtra("appAReceiver", resultReceiver);

        ComponentName cn = startService(serviceIntent);
    }
}

I call this code in the onCreate method of App A.

In App B, I registered my IntentService in the manifest like this

<service
    android:name="com.example.project.appBCommunicator"
    android:enabled="true"
    android:exported="true"
    android:process=":remote">
    <intent-filter>
        <action android:name="com.example.project.appBReceive" />
        />
    </intent-filter>
</service>

My code for my IntentService is this:

public class AppBCommunicator extends IntentService {

    public AppBCommunicator() {
        super("AppBCommunicator");
    }

    public AppBCommunicator(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ResultReceiver rec = intent.getParcelableExtra("appAReceiver");
        ResultReceiver sendingReceiver = receiverForSending(rec);
        String val = intent.getStringExtra("foo");
        Bundle bundle = new Bundle();
        bundle.putString("resultValue", "My Result Value. Passed in: " + val);
        sendingReceiver.send(Activity.RESULT_OK, bundle);
    }

    public static ResultReceiver receiverForSending(ResultReceiver actualReceiver) {
        Parcel parcel = Parcel.obtain();
        actualReceiver.writeToParcel(parcel,0);
        parcel.setDataPosition(0);
        ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
        parcel.recycle();
        return receiverForSending;
    }
}

For some reason App A is not finding the IntentService of App B (services.size() returns 0). Is there anything I am doing wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pink Jazz
  • 784
  • 4
  • 13
  • 34

2 Answers2

1

Your Intent is:

Intent serviceIntent = new Intent("com.example.project.appBCommunicator");

Here, your action string is com.example.project.appBCommunicator.

Your <intent-filter> is:

<intent-filter>
    <action android:name="com.example.project.appBReceive" />
    />
</intent-filter>

Here, your action string is com.example.project.appBReceive.

Your Intent does not match your <intent-filter>, because the action strings do not match.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Apparently my fix was to delete my IntentService file and recreate it under a different name using the Android Studio auto-generate wizard instead of creating it manually. I don't know why this made a difference, but it did.

Pink Jazz
  • 784
  • 4
  • 13
  • 34