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.
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.
As a requirement in Android OS phone, I am developing an app which will clean up a particular folder in a specified interval of time, say every 30 minutes.
I have tried this using services, check my other posting stackoverflow: file-cleaning-service
I am trying to find a best method to execute the file cleaning operation.
Should I use a service with broadcasting (or a Handler)?
Should I perform it within the app without using a service? The app can hide itself and perform task in the background. Is this approach is good? My point is why should I use a service if the service is just sleeping for a specified time, why same cannot be achieved in the app itself?
Is there any other method through which I can achieve this?
I do not want to wake up the screen, hence I do not want to use AlarmManager.
So please help me with best solution. Thanks in advance.
I just started working on an application for Android, to be specific - a service.
I would like to create a service (background service, forcing itself to run, hidden, so it can't be shut down). and I don't have idea on how to construct it. How it is then launched and how to make it fit to those requirements. It needs to start running from the Android start, so I guess it would be created similarly to system services (or as a system service).
You may ask why I need that - I would like it to periodically send some data to MySQL DB without any user interaction - therefore it needs to run continuously.
It would be really helpful if You could give me a great start, a tutorial maybe or some structure drawing with crucial information.
One more thing - I am targetting Android 2.3, but if it's impossible, I'd like it to be the lowest possible Android version.
Thanks in advance guys!
I would like to create a service (background service, forcing itself to run, hidden, so it can't be shut down).
Fortunately, this is not possible. Users have control over their device, not you, and they can shut down whatever they want.
It needs to start running from the Android start, so I guess it would be created similarly to system services (or as a system service).
System services are part of the operating system and cannot be created via the Android SDK.
You may ask why I need that - I would like it to periodically send some data to MySQL DB without any user interaction - therefore it needs to run continuously.
It does not need "to run continuously". That would be a singularly poor implementation.
If you want to do something periodically, use AlarmManager for the scheduling. The work should be done by an IntentService, so the service can go away when the work is done, so you are not tying up memory all of the time and making it more likely that the user will shut you down. If you plan on waking up the device out of sleep mode to do the work, you should look into WakefulBroadcastReceiver, or possibly my WakefulIntentService.
Alright, I found the answer myself, but thanks for help to any of You who contributed :)
I am aware that it's not the best approach, but I need to do it that way, so I found this for running a service and restart after stopped, also found that for start the service/application on boot.
Both tested and worked for me.
I hope it is going to help anyone who needs it.
Do you want this service to be executed daily??
You need alarm manager .. that will help you in to start you service with android start and will execute your service after specified time delay.. You can specify the time for delay also. Dont forget to add permissions for alarm manager in manifest file.
my knowledge of services in any operating system, is that they usually run in the background and perform whatever work they have to do.
but the first time I got familiarized with android services, I got confused.
it appears they only run when the application is working, and that for me, makes them no more then sophisticated threads.
do I have this all wrong? how do I make a service that runs when the application doesn't? (so that I can check for updates and create notifications for the user that will then lead him to the application if he chooses to open them).
does push notifications have anything to do with it?
Edit:
thank you guys for your answers so far.
my problem seems to be the fact that the service is only started officialy when the device is booted up. I do call startService when the app starts, but that doesn't seem to help. the service still dies when the app is turned off (unless it was booted)
also I never call stopService
If you are trying to implement a long running task that is performed in a (background) service, you have to start one or more threads within your service. So the service just gives you the opportunity to have an application context without having to have a user interface ;) you can consider it as a kind of container.
This page give you a nice overview about different thread approaches in Android. As you can see a service has not its own thread.
Anyway, in your case it seems that an AlarmManager is probably the better option. Running services for polling information all the time can be quite CPU and battery consuming (see this post for instance). So try to avoid having threads that run all the time.
If you can push information about updates from a server it's just fine. Check out Googles Cloud Messaging in this case.
Michael who commented on my question first was right in his comment about startService()
so it goes like this:
my receiver is only activated on boot, and uses an AlarmManager to
time the service to certain intervals.
what I did was to bind the activities to the service. if the service
was off and I binded it and unbinded it, then by the time the app was
terminated, there was nothing keeping it alive.
by simply making sure that the service was started properly with
startService if it is not already on, I managed to keep the service
alive at all times
also thanks to Trinimon who gave a very nice explanation on what
services are, and the importance of not overloading the CPU with
excessive polling. (which is kind of a trade off situation)
good luck to all :)
I'm implementing an background process that will update information my app uses.
I only want this process to update say once a day, if the process gets data newer than what it had before I want to present the user with a notification, exactly like twitter/gmail does.
I want the update process to run automatically, even when the main app is not open.
Is a Service the best way to go? I've been reading quite a bit about this, I figured a service running all the time for something that is only going to do work once a day seems a little overkill.
However I notice google run service for friendlocation and google+ services continuously on my nexus.
I've look into starting my service via the AlarmManager so its only started when required.
Some posts also suggest using the Handler class, I don't think this will work.
Just looking for the best practice here.
I figured a service running all the time for something that is only going to do work once a day seems a little overkill.
Absolutely.
I've look into starting my service via the AlarmManager so its only started when required.
This is the correct answer.
If you only want your code to be invoked if the device is on, implement an IntentService, do your work in its onHandleIntent(), and have AlarmManager start up the service on your desired schedule.
If you want your code to force the phone to wake up, you can do that, but you will need to use a _WAKEUP-style alarm, and you will probably want to look at my WakefulIntentService, designed to handle this pattern.