Android service shutting down - android

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

Related

Stop background service only if no more work

I use a service to process intents with ContentResolver in the background.
Each new intent calls starts the service and the service stops itself once the processing is over.
This creates an issue where new intent starts a service run but the previous service run is still processing, resulting in killing the new run before its processing is over.
I thought about adding some kind of static 'nunInstances' in the service and stop it only of it's 1.
Another option is leaving the service running. I've tried looking for information about it's validity and side effects but came up empty.
You can use an IntentService, which acts as a work queue. You can send it work to do with startService() as many times as you like. The work is performed serially, one after another. When the last work unit has been performed the Service stops itself.
Or you use a regular Service and queue the work yourself in onStartCommand(). When you have no more work to do, you call stopSelf().
NOTE: Android will not create more than one instance of a Service. Your concern about having multiple instances of the Service running is not necessary. If something calls startService() and the Service is already running, Android does not create a new instance of the Service. It just called onStartCommand() on the running instance and passes the Intent as a parameter.

how to keep my background service active when android system kills my app?

I have a background service which runs in background to track screen on and off events. its started as below :
Intent intentService = new Intent(context, UnlockdService.class);
context.startService(intentService);
And it is returning Service.START_REDELIVER_INTENT in the method onStartCommand. But still when android system kills app this thread dies as well.
How can I prevent it?
I think it's a better solution to have a BroadcastReceiver instead of a Service. In your onReceive, you could then start your service to do its work, and then the service could self terminate - maybe you could use an IntentService if your work is one shot.
If you still want a Service, you can make it less likely to be killed by having a permanent notification in the notification bar, but that can be bad from a user perspective. I strongly recommend going the broadcast receiver route.
The solution was so simple: Returning START_STICKY from onStartCommand() did the job.

How to stop intent service that is launched in another process from activity

I have made an Intent service and that is working as expected, And I know that intent service does it works and stop itself after its working , so there is not need to stop it. I have made my intent service as you can google it , but I have launched it in a separate process. the code goes like this in xml
android:process=":MYPROCESS"
so in my app , I launch the intent service in the following way on button click
Intent intent = new Intent(DownloadService.ALARM_SERVICE);
intent.setClass(this,DownloadService.class);
/* Send optional extras to Download IntentService */
intent.putExtra("url", "http://myurl.com");
intent.putExtra("receiver", mReceiver);
intent.putExtra("requestId", 101);
startService(intent);
this is just to give you idea How I am starting it , its normal.
What is Happening
I have made a button to stop a service , my this service is downloading something. so What I did , I killed the process , but that start the service again .
As I wanted my service to start again when it is killed by the system or when app is killed so I have set intent to redeliver like following
setIntentRedelivery(true);
So it is amazing situation :so simplifying my question as following
I want to stop service to not to start again , but when it is killed by system or it is killed when user removes app from recent apps , then service should start again (which is going good).
Please tell me how can I achieve this .
In order to do this, you'd have to further modify your custom DownloadService derived from IntentService. The IntentService base class is automatically starting a thread and servicing your Intent there, shutting down the thread and the service when it has no more work to do. You have 2 choices you could use to stop the service:
Implement onStop() in your service and have it set an internal (synchronized) shutdown boolean. In your onHandleIntent() you could check for this flag and prematurely exit the handler and possibly call stopSelf() to prematurely exit.
Alternatively, you could add your own onBind() implementation and have your Activity bind with the service. Then expose a binder method (see docs on AIDL) to be called by your Activity when you want to exit the service. Like above, your service's onHandleIntent() would need to watch for this early-exit type flag and cause itself to stop prematurely.
Note that in either case the service's process will most likely not get killed and this is normal. Android keeps processes cached and in a ready-to-go state so it is very responsive when new requests to start its components arrive. This does not mean your service is "running". It just means the process which will host the service is already created and ready to go.

How to set the priority of android service?

Is there any way to set the priority of android service? I want to run the service in background after destroying App's main actvity. So i am returning "START_REDELIVERY_INTENT" from my service. But restarting of service is taking some time (around 1-3 minutes). I want to restart the service instantly. Is there any way to set the priority of android service.
FYI, I am creating service using startService and passing an object through intent.putExtra().
Below snapshot is taken from setting/apps/running apps by my android device, which shows that app is restarting... My app is real time specific, so i want to restart it instantly...
You cannot control this behavior of Service as it depends on the OS and available resources. However, for a continuously running background service, I would suggest you to return START_STICKY - which tries its best to re-run the service once its destroyed due to any reasons.
The priority depends on the component type and the current app that the user is using. The highest priority have the current Activity that is displayed to the user and all foreground services. Foreground services must have a notification that cannot be dismissed. To create a foreground service you have to call startForeground(...) method from the Service itself passing the id of the notification and the Notification object itself. You can use the id if you want to update the notification later.
I recommend you to make the service bound and bind your activity to the service. This way the you can start and stop the service in your onResume and onPause methods.
The only way to increase service priority in this case is to make it a foreground service.

Android Background Service That Runs Forever

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?
..
..
}
}

Categories

Resources