Ok, so I'm developing an Android App with news. When user runs App for the first time, a separate thread runs, then a infinite while( true ) loop starts, inside a loop a connector downloads text from the Internet to the notification and sends this notification, then sleeps for 8 hours. Which way is better to make it working best, a thread with way as above or alarm manager? Or, maybe there is a different and better way?
For now I've done two ways for testing, both work good, but I have no idea how to check which one is more efficient, which won't be killed by android, which eats less resources, etc.
And second question, is there any way to restore the loop when someone kill app? I was testing with Advanced Task Killer Free and an app Flashy (Flash Player Loader). I killed the Flashy, but 5 seconds later app was running again, so it propably is possible, but how?
And for those who think I'm developing annoying ad - no, App which I'm developing just reads news from the Internet.
Hope somebody helps,
Thanks in advance.
Thread vs AlarmManager
AlarmManager
As per Android doc
The Alarm Manager is intended for cases where you want to have your
application code run at a specific time, even if your application is
not currently running.
So the advantage you get here is you can perform specific task in future even if you application is not in running state.( Here you can proudly call yourself good android citizen since you are not residing in android memory to get your task done).You just tell android I want to get this task executed at particular time,Android will automatically start your application at that particular time even If it is not running.
If you want to acheive same thing with thread then your thread should be alive till the time task does not start executing.( The disadvantage will be android will not kill the thread till the time process is alive and your tread will unnecessary eat up memory).
Hope this will clear your doubt.
In your case I would definitely use the alarm manager.
As a general rule of thumb, if your application "sleeps" and routinely checks for new content in long intervals (8 hours is a long time), you should use the alarm manager.
This way your app doesn't need to run in the background, thus battery life is conserved, and the application's functionality will not be affected by Android killing the service in cases of low memory.
Regarding your second question - if the Android system kills a service due to low memory, it will restart it as soon as possible. However if you kill the service manually then it should not be restarted, even if it is possible through some hack.
Related
Search engines and Android developer website didn't help and I guess you can help with my problem.
I want to make an app for personal use, which is supposed to run all the time on my old tablet (powered all the time). The app will have several features requiring user interaction but independent of those, it should run a background job to check something continuously (real time!) for instance sound detection. It should also always try to connect another device on the network.
That means that job needs to run almost eternally without being killed. Some comments I have found suggested AlarmManager or BroadcastReceiver. But those are triggered by very defined triggers (either time or broadcast). I don't want that, because it should perform its task continuously all the time. This background job should also be able to communicate with the main Activity of my app to report what it is doing and allow user to interact with it (change settings of the job for instance).
Do you know any way how to accomplish this? Is IntentService correct choice for this (hoping that it won't get killed or maybe I should let the Activity to restart it?)
Thanks!
Do you know any way how to accomplish this?
Build your own custom ROM, with a modified version of Android that contains your code as a native Linux daemon.
Otherwise, what you want is technically impossible.
You can come fairly close by using a foreground Service (not an IntentService) and returning START_STICKY or START_REDELIVER_INTENT from onStartCommand(). Android may terminate your process from time to time, but it should restart your service automatically after a short while. That service can use its own background threads to do whatever it is that you are trying to do.
I made an android application with a runnable that checks something ever minute.
But the problem is the application goes [DEAD] after a couple hours without an error messages or anything.
Anybody have any idea what the problem could be?
That is not a problem, that is actually expected behaviour.
The lifecycle of all apps is managed by the Android OS. It decides whether to terminate an app in order to free resources and keep the system responsive. Apps that are currently is use have priority over paused and background ones. I think in your case the OS just decides to shut down the app because it thinks it's not needed anymore.
There are ways to get around this, but it all depends on what your app actually does. I don't know your implementation details, but may want to look into sticky Services or the JobScheduler in order to achieve what you want. Keep in mind that there is no such thing as a perpetually running background task that comes out of the box in Android (not should there be one) and usually implementations have certain limitations.
It could be the Android OS itself closing the app. If the OS requires more memory it will start to kill of other processes that have not been used for a long time (i.e. interacted with). You haven't stated how the check happens but it shouldn't be done directly within the app, but it sounds like it is something that a background service should be doing the work which would likely prevent this from happening.
You should NOT use Runnables for background processes, as they get suspended/killed by the OS as soon as your app goes off-screen.
If you want to let some processes run regularly, you have to stick with AlarmManager / BroadcastReceiver combination
I want to perform action/event when application killed from task manager or any other app. Is there any to perform action when application killed. My application is running in background like service. If i terminate the application then main service stop . I want to start it again.
No, there's no reliable way to know if your application was killed by a another process. The whole point of "killing" an app is to terminate it as soon as possible, without letting it run any code.
== Do not actually use the following suggestions in production application. They are here purely as potential technical solutions, but in general are not a good idea for apps running on end user devices. ==
It might be possible to use IBinder.linkToDeath() from a secondary application, which acts as a monitor for your primary one. However, you will have to convince the user to install the secondary app as well. If you can do it, you could establish two-side monitoring between the two apps, and have one of them restart the other if the second is killed.
You could also attempt to set an alarm through the AlarmManager that fires every so often, to restart your application if it happens to be killed. However, if your alarm period is too big, you risk having certain period of time where your app is not running. And if your time period is too small, most likely your app will not be allowed by Google in the Google Play Store, and the malware app analysis on the phone (JB+) might kick in. Also, alarms that kick in too often will keep the device awaken, and drain the battery very fast.
If you kill some process, you just kill it, so it stops working immediately. There is no event sent to the application.
I looked for the same thing and the answer that i found is : NO, the application does not go to OnDestroy() or anything like that.
I finished an Android program. It uses thread. No service.
I would like when the user closes the program with the BACK button
to give an option for the program to keep working in the back ground, or part of it.
The question is what is the best way to take and how to implement it.Is SERVICE the only
way to go or there is another way?
I have seen tracking programs do it.
Yes , service is the best choice of all to keep your application running in the background.
But also remember that keeping a service running does not mean its guaranteed to not getting killed.If resources are too low, Android system might kill your service too.
Please go through this and check Process lifecycle under it.
As you know ,phones are very limited in the amount of resources, its ideal that you do minimum amount of work in the background and also if you require to download something in the background after a specific time consider using AlarmManager instead of polling in the service.One purpose of creating a thread is not blocking the UI.Remember that a Service runs in the UI thread itself,So starting a thread inside a service is a better choice if you want to have long running background work.
I'm working on an Android app to be used in an experiment we are running in our lab. The application monitors the users movement using the accelerometers, performs calculations on this data at a regular interval, and writes the results to a file.
I'm currently having a very difficult time trying to find a way to run this process for the 15-20 minutes our trials require without it being killed. Despite declaring applications persistent, trying various service approaches (startForeground(), START_STICKY, ..), etc... I cannot seem to keep the Android OS from deciding to pause or kill my service/process.
I've done some research and the only advice I can find is how to set up your processes to gracefully recover from being killed, however I cannot afford to have gaps in my data and therefore need this process to run continuously.
Could someone point me in the right direction?
From documentation I get the impression that if you want to have a service that will only be killed in extremely low memory situations you need to:
startForeground() (you have done that)
START_STICKY (you tried that too) or START_REDELIVER_INTENT (restarts service if it is killed, but that leaves gaps in your data)
run all its processing in a separate thread
use Context.startService() to start service
The only sure way is to have it as a system daemon.
The best solution I could come up with for my problem is increasing the screen sleep delay on the device to 30 minutes and pray no buttons were pressed during our trials.