Android Background Service That Runs Forever - android

I need to make an app that will run forever at the background and if needed it opens activity for user UI. I made an Activity that is the main activity which all it does at it's onCreate is to call to startService(new Intent(this, MainService.class));
The problem is that after the onStart command of the service is being called the MainService class becomes null and it is stops running.
Do I need to start the service in a different way? Should I start a new thread for the service?
Thanks,
Nahum

if your want to continue your service running though app gt close.then you need to return STICKY like that. and also need to use BroadcastReciever.and your service will not run continuesly because if system need to release memory then it will kill but yes you can restart your service for sure. So i will suggest you to go through whole documentation and stuff of service Service and
Broadcast
it may helpful for you. and one thing there are preferences which process will kill first by system and so on..check it out.

You need to make a new thread in your service and start this service using command startForeground.

If you want your service to run forever, your code needs to be able to run forever too
onStartCommand {
while (1) {
..
..
//call your activity?
..
..
}
}

Related

Keep a Service started at BOOT running even if Application stopped

I am working on a app that during boot time starts an activity that logs in to my server (needs an activity to log in through facebook) using a service (initiated with startService). This service establishes XMPP listeners and does nothing after that, just waits for connection. I want this service to run all the time the device is up.
The problem is that the activity stops after a while and my service is also stopped. The service returns START_STICKY so I was expecting it to hang around. Also the service doesn't do anything except wait for connection.
The activity has the properties:
android:excludeFromRecents="true"
android:noHistory="true"
android:launchMode="singleInstance"
so that it does not show up in the task list (when user long presses the home button).
The activity is stopped when the user long presses the home button and the service also ends. I am thinking its possible that the application exited, that's why the service also ends. I could not find any way to keep the activity from not stopping. Maybe its stopping because of the above properties.
So what can I do to keep the service running all the time. How can I keep the application from being removed. I read somewhere that if I keep a while loop running in the service then START_STICKY can keep the service around??
I can use AlarmManager to start the service but I don't want it to stop easily and then have to restart it every time.
I don't want to run a foreground service. I can not run the service in a different process since I am using existing code that does not do IPC. Any help will be appreciated. Thanks.
There are two things to keep running a service indefinitely; create the service using startService() and return START_STICKY from onStartCommand(). You seem to be doing this both. With these two steps, the service may be shut down by the system but it should restart almost immediately.
The only suggestion I have is to create a separate thread in the service. This is because by default, started services run in the application main thread. If the service is constantly doing certain task, it may block the main thread and kill the application. Google doc has an example of implementing this:
http://developer.android.com/guide/components/services.html#ExtendingService

Restart a background service if stopped

my question is simply about how do we restart an android service that runs in background in it's own thread from an activity if the service stops itself after completing certain tasks.
Simply call startService() from your activity:
http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)
Make sure you are extending Service, not IntentService. And then start service in STICKY mode. It should remain active that way. Even if it dies for some reason (system may kill it etc.), it should restart on its own.
Read this link: START_STICKY and START_NOT_STICKY

How can I run android background service "forever"?

I looked through all the posts about it in here and in android documentation about service.
I still can't understand how to do it.
I want to write application that collects battery data all the time event when the UI is closed. I found that I need to use a service for this with BindService & StrartService so the UI Activity could communicate with the service and the service could also run by it self in case the UI activity closes.
The thing that I can't understand is how do I make the service run "forerver" even when I close it with android app manager. For example: Whatsapp application even if I close it with the app manager in the moment that someone sends me a message I still get it and whatsapp turns on.
Do I need to use thread (runnable interface with run function) or what?
You may try to override onStartCommand() in your service and return START_STICKY; to let the system know your service should run until it terminates itself.

Android service shutting down

This is my first time experimenting with android services, so I am a little bit lost. I am developing an application that requires a service to run in the background at all time. This service is initialized from an onclick event in the main activity. To start the service I use the following code:
Intent Test = new Intent(this, testService.class);
startService(Test);
In the service I basically have two things. In the method onCreate I initialize a timer and every 30min it opens a new thread and checks if the server has any new data. While on the onStart methods I register a Receiver.
After a couple of hours the service is being killed, is it possible that the garbage collector is removing the service? My suspicion is that the way I am initializing the service it is still binded to the activity "main" process. What can I do to make sure that the service keeps running?
Thanks
You need AlarmManager instead of timer for this task.
OS can kill any normal priority or user defined background service when OS required resources, so if you want that your service run always then you can set your background service to foreground or and if you don't want to let your phone on sleep mode, you can use wake lock...by these you can set your service on always on mode.....
Thanks

How to register a Service that will run all the time

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.

Categories

Resources