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?