I am interested to know how WhatsApp runs in Android background system even after cleaning it from ram cleaner.
I made an Android app in which I started service and broadcast receiver but when I cleaned it using ram cleaner, both got stopped. Even sometimes push notifications are also not received when app is not running in background.
So, I just wanted to know that how WhatsApp manages all this. I am just giving an example as WhatsApp because I found its system amusing.
If you return START_STICKY from onStartCommand(), the system will automatically restart the service once it determines that it is not resource strained. Which means it will probably restart immediately if you have killed it using an app killer.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
http://developer.android.com/reference/android/app/Service.html#START_STICKY
Related
i want to run a script even if the Task is destroyed. That works fine but is it possible to keep this service running, after the user destroyes the app?
I read something about binding the service but this is not working for me.
No. If the user uninstalls the app, all components are destroyed and removed from the operating system.
However, you can make a Service automatically restart after the app is killed (but not uninstalled) by starting through context.startService(Intent), and returning Service.START_STICKY in onStartCommand()
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
I finally finished my app, but it doesn't work properly on my phone - it seems that the app is always being re-created when I open it after the device has been put to sleep, which (in spite of my best efforts) causes a LOT of trouble for me.
Looking for a way to prevent the app from being killed, I came across startForeground... which, as I understand it, applies only to services (if not, I can't figure out how to apply it to an Activity - I've tried).
Is there an option which will save me from having to make a lot of serious changes? (I barely understand what I've done so far as it is...)
I've read somewhere that having an ongoing notification work, or worked until froyo - I'm not even sure.
If this is true, would I have to be concerned about the energy consumption? I am using an AlarmManager with a partial wake lock on the BroadcastReciever, so until the alarm is fired, I don't actually need to be doing anything - just keeping the app alive.
If you return Service.START_STICKY; in your Service's onStartCommand, it it should restart in case your app gets terminated. So if you have any code that you want to run for a longer period of time, put it in a service.
Here's an example:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerAlarmBroadcast();
return Service.START_STICKY;
}
I wrote an App that will run process in a long time, possibly never ending until the user stop it.
But I afraid the App will get suspended when the mobile/tablet screen is sleep, or user switch to another app so that my app long running task is getting suspended.
How do I prevent that?
You can probably start a Service here if you want your Application to run in Background. This is what Service in Android are used for - running in background and doing longtime operations.
You can use START_STICKY to make your Service running continuously.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
and see this :http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
and This is an example of background service:
http://marakana.com/forums/android/examples/60.html
What you want is implement a Service.
It will have that behavior that you are asking.
You should use a Service and return START_STICKY on the onStartCommand() so that the system will not terminate your app. Even if it is terminated, the service is automatically restarted.
I have a function that reads the content of the Android clipboard every two seconds and communicates all changes with a remote server.
This works fine in the app is opened. But I need to be able to continue to log the clipboard changes after the app has been closed.
So I tried an IntentService but it doesn't appreciate long loops.
How can I run my infinite loop in the background?
I have a function that reads the content of the Android clipboard every two seconds and communicates all changes with a remote server.
Polling? You are doing it VERY wrong. You should use OnPrimaryClipChangedListener instead.
EDIT
communicates all changes with a remote server.
I just realised you are basically doing some bad things there and I am bit sceptical believing your users aware you literaly spying on them? Is your app in Google Play if so what's your app package id?
IntentService should be used to process single "request", from another application component, at time.
Use started service which should be more suitable for problem you describe. Started service runs until you manually stops it or until system gets out of resources and kill whole process.
Use START_STICKY as return from onStartCommand() method to automatically start it again when system kills it.
If you also start it in BroadcastReceiver with ACTION_BOOT_COMPLETED action service will be started after device boot.
Implement a Service and make it sticky by returning START_STICKY in onStartCommand (see Documentation):
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
Can I run a background service Indefinitely ? Will android kill my service if i run it for indefinitely ? How Facebook android application keep running in background for a long time ?? please help me to know about it .
You can use for that these features:
1) Auto-restart service after reboot (Start intent after Reboot)
2) Sticky service mode (http://developer.android.com/reference/android/app/Service.html#START_STICKY)
These features both helps to leave your service started all time as possible.
Android will kill your service if it's running out of memory, but you can do a couple of things to recover from it.
The first thing you can try is to use foreground services, a foreground service is a high priority services that won't be killed unless is completely necessary (note that these services increase battery consumption). You can find an example here using compatibility with older devices, otherwise you only need to call startForeground inside your service.
Another thing you can do is to use some flags in your service to restart it when it gets killed by the OS. You can use 2 different flags (depending on which behaviour you want to reproduce).
START_STICKY will restart your service with an empty intent so everytime you have to recover the data you need to run your service.
START_REDELIVER_INTENT in this case your service will be restarted with the last intent information (it could be that you run your service several times with different information when you want different behaviour).
The flags can be used as follows:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Do your service work
return START_STICKY; //or return START_REDELIVER_INTENT;
}
Depending on what you need use one or another.
Hope it helps :)
There's no way to absolutely protect a Service from being killed.
If you return START_STICKY from onStartCommand(), then even if Service is killed, android will try its best to start it again, when resources are free.