androidandroid-activityservicebindservice

Service starts activities from different Apps


I developed two apps(can be more in the future) which are using the same service. When I start one of the apps they should start the service(if not already started) and send the name of an activity to tell the Service that the app is available.

The service is located in a third project, which is implemented as a library in the apps. The service listens in the Background for an event and start one of the apps.

The problem now is, how I start the service.

I tried already startService and bindService:

startService:

Intent intent = new Intent(this.getApplicationContext(), DriverBackgroundListener.class);
        intent.putExtra("className", "com.example.testProject.Activity1");
        this.startService(intent); 

Starts a new Service for both apps, because the service is not located in the same app. So 2 Services are running and listen for the same event.

bindService:

I implemented it with the help of a Messenger. (https://developer.android.com/guide/components/bound-services.html)

The Problem here is, that the Service will only run if minimum one Activity is bind to the service. But I need to finish the Activity so the user can use the device normaly. But that will unbind the service and shut it down.

I also have to run the Service after a reboot. But I guess that will work if I create a BroadcastReceiver which starts the activities after the device is booted.

Have anyone an idea how to handle this?

Thanks best regards

Fabian


Solution

  • If you start service and then bind to it with flag 0, it still work even after you disconnect from it. You can try to connect to service first and make a check in onServiceConnected(), and if not start service. Also take a look at AIDL https://developer.android.com/guide/components/aidl.html.