Can we create an IntentService as STICKY? - android

The document says intentservice calls stopself implicitly. So was wondering if intentservice can be made Sticky.
Thanks

sticky is like a property (it´s not a property) that you can give to a service, and it gets activated after the system shuts it down because of low memory, when the system has enough memory it will restart the services that return START_STICKY on their onStartCommand, otherwise the will remain off. In IntentServices you cannot return START_STICKY (at least i haven't found the way) but what you can do is:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
currently i´m using it in my app and it works well

Related

Android Service doesnt restart by itself

In my application I have an activity and a service (extends IntentService ). the service's onStartCommand looks like below
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
My onHandleIntent method:
#Override
protected void onHandleIntent(Intent intent) {
while(continueLoop){ //continueLoop is controlled by the Binder
//Do stuff
}
}
I also bind to the service from activity, so I can break the infinite loop. I started the app and it's service, and then started other applications, after a while my Activity got stopped and destroyed, so is my Service. When I close the other applications using task manager , the service doesn't start by itself.
I waited and then launched my app, as soon as activity is launched service also started. I thought the android system will restart the service automatically when memory is available. Am I missing something or should i wait longer.
Thanks in advance
If you read this IntentService you'll see that
onStartCommand(Intent intent, int flags, int startId)
You should not override this method for your IntentService.
Instead
The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent).
Per the IntentService documentation:
Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
If you are binding to the Service and/or controlling the lifecycle of the service yourself, then you should use a Service and not an IntentService.

Android keep BroadcastReceiver running in background

I did a service starts a BroadcastReceiver. The BroadcastReceiver is working only if the app is shown i recent apps screen. When I remove the app from recent apps screen the BroadcastReceiver stops.
How can I save the BroadcastReceiver always in background?
Your service might get killed along with your activity
You should add process=":background" in your manifest file within the <service /> node.
Or, alternatively,
In your service, you must return START_STICKY
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
declare the BroadcastReceiver in its own class, and add it to the Android Manifest, instead of registering it with some context.
This should be a simple starting point for you
Whoops I'm wrong. http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED

Is this the right trick to keep a service alive even under low memory pressure?

I used the following code to keep a service alive:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
keepServiceAlive(this, 2222);
return super.onStartCommand(intent, flags, startId);
}
#SuppressWarnings("deprecation")
public void keepServiceAlive(Service service, int id) {
Notification note = new Notification(0, null, System.currentTimeMillis());
note.flags |= Notification.FLAG_NO_CLEAR;
service.startForeground(id, note);
}
But sometimes the service was still being killed. Could you help me confirm the correctness of this piece of code, or would you offer a better solution?
You should not create a malformed Notification object. Android 4.3 will now show it's own Notification to the user when it detects this hack and the user will be informed that your service is running. If you really need it to be in the foreground, give it a proper icon and let your users know about your service, and explain why it's necessary. You might even consider allowing them to decide whether to have the service run as a foreground service via SharedPreferences.
See this blog post by CommonsWare

Stopping an IntentService

Does anyone know if there is a way of stopping an IntentService without it finishing its work thread and stopping itself?
Simple question, but I couldn't find the answer in the documentation. Is there a simple way of stopping it?
Thanks
bevor a message to a service is enqueued onStartCommand is called. which forwards the message for queueing. so you could just override onStartCommand, something like that:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals("Stop"))
stopSelf();
onStart(intent, startId);
return START_NOT_STICKY;
}
cheers
You should be able to call stopSelf();
http://developer.android.com/reference/android/app/Service.html#stopSelf()
I currently stumble upon this requierement for an app i am working on. I will try using onStartCommand to send a message to the Intent Service to stop working (for example, setup a boolean flag stopWork = true) and evaluate it during the working job or before the next queued task. The IntentService wont stop inmediately but will skip all pending tasks. Hope it helps. Gonna try it myself also.

How to restart a killed service automatically?

When a service has been killed, how to restart it automatically?
sometimes without even calling onDestroy()
I inherited an IntentService, so I had to be gentle.
It worked for me when I overrode onStartCommand() but
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
That is, let the parent do what it should and return START_STICKY.
Overrides the onStartCommand() and give START_STICKY or START_REDELIVER_INTENT (depends on your needs) as the return value. The system will then make sure to restart your service until you explicitly stop the service.
http://developer.android.com/reference/android/app/Service.html#START_REDELIVER_INTENT
http://developer.android.com/reference/android/app/Service.html#START_STICKY
If you have your service killed, the system will try to restart it later.
Read more.

Categories

Resources