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
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.
Related
I am developing an Android application where I start a Service via an Intent. The service opens a connection and reports its success to an Activity. Afterwars the user shall be able to start a data transmission via Button click. This is also done by the service.
Is it the best way to send another Intent to my service? And does a service always exist only once or is another instance created of it?
No matter how many times you start the service, the service will be started only once and at any time only one instance of the service is created. If you start the service multiple times using startService() method then onStartCommand() method will be called multiple times.
The best way to communicate the service from the activity is to bind the service(bounded service).
I have this app that I want to run at phone start up (As in after the GUI comes up) but I want it to run in the background(like a service)
I know this is possible, since most of the android system is like this.
Any tips would be wonderful!
Create a service in your app. Bind it to an Activity if you wish.
Subclass BroadcastReceiver to listen to the Android.BOOT_COMPLETED system broadcast.
then start your service.
I have a broadcast receiver listening called PACKAGE_ADDED and an other broadcast receiver listening called BOOT_COMPLETED. The bootcompleted broadcast receiver starts my service.When the new app is installed, I want to send a message to my service .The first solution that came to my mind was to start the service again with
intent.setAction("NEW_APP_INSTALLED");
startService(intent);
without stopping the service and check the intent.getAction() value in theservice.onStart() method. If the result is NEW_APP_INSTALLED, then call newAppInstalled().I don't think this is an elegant solution.
Is starting the service repeatedly a problem? And what happens when my activity binds to it via ipc(aidl) while fetching data and the new app installed broadcast receiver starts it again? Lastly what is the best way to solve my problem?
Is starting the service again and again problem?
Lastly what is the best way to solve my problem?
It is perfectly fine for you to call startService on an already running service. And you can either do it the way you suggested or have two different services (one for boot, one for new_app) or you can register a BroadcastReceiver in the service after it's started, but that wouldn't be effective because then if you try to send a message to it and it's not running already, it won't get the message.. I prefer one service as you suggested and using startService.
And what happens when my activity bind it via ipc (AIDL) fetching data
and new app installed broadcast receiver starts it again?
Well, I don't know anything about AIDL, really. This might help. That page does state "Most applications should not use AIDL to create a bound service". This is because it makes multi-threading needed and makes it more complicated.
Please let me know if I failed to answer to your satisfaction - though I can't really elaborate on AIDL specifically because I don't know anything about it.
Scenario:
I start a service at boot time.
I want to start the same service at main activity manually.
I got 2 Questions:
1. How to ensure my manual service started is the same as the one at boot time.
2. how to retrieve those running services
Thanks
I start a service at boot time
Why?
How to ensure my manual service started is the same as the one at boot time.
Use the same Intent structure.
how to retrieve those running services
There is no such concept in Android. For starters, there will only ever be one copy of your service running. If the service is already started, and you call startService() again, it does not start up a second copy, but delivers the Intent to onStartCommand() of the running service instance.
I would like my android app service to run all the time.
that is -
1. right after installation,
2. on boot
3. if its closed - it will be relaunched -
how do i achieve all of the above code-wise?
thanks!
I am not putting the code here however you can easily find it.
Right after installation use the default activity to launch the service, in case you do not have any UI then create an activity without any UI (no setContentView) and in its onCreate start the service.
You need to create a broadcastReceived that listens to ACTION_BOOT_COMPLETED and call that as Service Manager. On Receiving the broadcast in that receiver just start the service again.
Make your service as foreground and that should ideally take care of this scenario.
You shouldn't use the Service foreground feature! The best practice in current android version is to return START_STICKY from your Service's onStartCommand(). It will cause the Android system to re-launch your service.
Regards.