1. I am trying to make a timer which will run even when the app is killed. I have found a solution to use a service. But the service restarts every time i kill the app.
According to my research the workaround is to put the service in global process, i have tried that but does not work for me.
I am using android studio 2.0 and API 23 as a target.
Even if i manage to get the timer working in background, it will still stop if the device is turned off or in case of dead battery, so would it be a good idea to run the timer on server?
Please help.
Running timer in service makes battery low very fastly. I dont know your requirement but you can use datestamp according to device time.
Related
I'm creating an app that captures location every 30 seconds. To do so, I've a foreground service and a handler in it that gets the latest location every 30 seconds. The app is working just fine for many devices with stock OS. But, on some devices like OnePlus, Panasonic, Vivo etc. the foreground service gets killed by the OS(sometimes the app too) even after changing the battery optimization status and the doze mode. I know that it is not possible to create a service in Android that does not die. Is there any way I can achieve what I'm trying to? Please let me know. Thanks!
First of all, yes you are right the operating system will stop services when resources are limited, so to get around this you state the type of the services while creating them as the following:
START_STICKY, START_NOT_STICKY, START_REDELIVER_INTENT...
read the last part of this article it talks about when to use which of them
https://android-developers.googleblog.com/2010/02/service-api-changes-starting-with.html
Service.onStartCommand() callback that allows the service to better control how the system should manage it. The key part here is a new result code returned by the function, telling the system what it should do with the service if its process is killed while it is running.
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.
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.
I am using a timer in my application for repeated task. By default, the timer should repeat the task with a delay of one second. I am starting the timer when the application Starts. All this is works perfectly. Also,When I came out of my application and open some other application, the timer keeps running.
When I open Google Maps application, my timer stops running. I don't know why this is happening. I googled and found from the activity life cycle that, if other applications needs memory, all processes will be killed. This is what happening in my case, I guess.
I do not want my timer to stop. It must run always till the user uninstall my application.
Why the above problem occurs?
How to achieve my requirement?
Does Using services will solve the problem? If so, Is there any way to keep timer always ON without using services?
Does Alarm Manager be helpful? Even if the user restarts the phone, the timer should work properly.
Yes, a service will solve your problem. The persistence of an Activity is governed by its lifecycle, so in the end, you have no control of its execution. A service is persistent in that it will not be shut down by the system. If you are doing extensive calculation, however, be warned that the OS will not be as generous with resource allocation as it is with an on-screen activity.
In short:
1) Well, from the information you've given, I suppose you drew the right conclusions.
2) Through a service.
3) Yes, it will solve your problem, no, I don't think there is another way to do that, at least not from within an activity.
4) If you're not actually asking about the persistence of a Java.util.Timer but for a way to have a piece of code executed at certain times, this might be the better (/easier) solution. I don't know how well it works for rather frequent timings, though. A service can be resumed on system restart, so no worries about phone shutdown. You can subscribe to the startup event.
I did a search before asking so please don't tell me to do that.
I'm relatively new to Android so I get confused easily.
I'm making a simple app that changes the ringer volume according to time. For this purpose, I know I need a service that keeps running in the bg and monitor time. However, I really don't know how to approach this. I have the Professional Android Application Development book but haven't found anything that helps in there.
My question:
How to constantly check time without destroying the battery
If my code is needed (as proof of me actually working on this), I'll post.
You don't need a service. Use the AlarmManager class. Its like an alarm clock and it exactly what you need for this type of app.
need a service that keeps running in the bg and monitor time
No. Actually that's not how to do it. Services on android are different than your normal windows service/unix daemon. They should do their job and then stop themself until they get started again - to save battery.
You should start your service at a certain point in time by using the AlarmManager, it sends the launch intent to run the service. When the service is finished doing what it's supposed to do (change the rintone volume here), use Service.stopSelf() to kill it.