So, I want my app to create a Service that will initially run in the background and do some stuff. This background process will never stop. It will constantly keep running. The ONLY way the background process can be created or destroyed would be through the app. I understand that there are endless possibilities to kill a process. I guess I want my app to be able to tell me whether or not the process is running and retrieve real time information from that process. Along with that, be able to start and/or destroy the background process.
So, let's say that you were to open the app and start/create the service. Even when you close/terminal/call onDestroy for the app, I still want the background process to be running. The only way to destroy this service would be to re-open/re-create the app, and destroy it.
Does Android allow something like this? Is there a way to get around this?
I was going to create a IntentService
and make it run an infinite loop. Though, my only problem is how to obtain information from the IntentService
. From what I've learned, an IntentService
is created and kills itself when it's done.
I'm still new to Android, so don't hesitate to be specific and/or remedial.
You dont need to make 2 apps for that, in fact, if your requirement is to only be able to stop/create the service from the main app it will only overcomplicate things (such as that you dont need aidl at all since its all in the same app) , that goes for the above answer (cant reply yet!).
Just create the app and the service class even on the same package as the other activities. Then on the manifest xml register the service like this at the application node:
<service
android:name="com.example.yourpackage.Service"
android:process=":remote">
<intent-filter>
<action
android:name="com.example.yourpackage.Service" />
</intent-filter>
</service>
What you are doing with the android:process=":remote" tag is explicitly setting the service to run in a different process (by the name of 'remote') than the rest of the app while still being part of it.
Then, to start and stop it simply use these from your activity:
startService(new Intent(this, Service.class));
· · ·
stopService(new Intent(this, Service.class));
Make sure you read the documentation about services (and broadcastreceivers) anyway, but that will give you a general idea as to where to aim and what NOT to do to overcomplicate.
I've been working with services lately so im fresh on the matter, if you have any questions let me know here!
<<<<<<<<<<<< EDIT : >>>>>>>>>>>>>>>
Ah I think I get you now.. if you dont want the service to keep running after the "app" is finished just forget about the :remote part. That way they will both share same app lifecycle (but not context lifecycle as they have different ones), sorry if that part confused you and thanks for the vote!.