I want to update the location every 5 mins till the lifecycle of the application. I know how to update the location. What I want to know is whether the requestForLocation is a blocking call or not.
I can do this in Service class but Service is run on main thread only and also Service can be stopped by the system anytime. I do not want this to happen. The location update should work till the application is in memory. If the application itself is killed then obviously the Service also gets killed.
What should be the approach for implementing this?
What I want to know is whether the requestForLocation is a blocking call or not.
There is no method named requestForLocation() or requestForLocationUpdates() in Android. If you mean requestLocationUpdates(), this is not a blocking call.
I do not want this to happen.
Users and the OS have the right to terminate your service whenever they wish. In this case, it's a good thing, because your proposed plan wastes RAM for no benefit.
What should be the approach for implementing this?
Use AlarmManager and a service akin to my LocationPoller, so you can get your location data, do something with it, and shut down until the next period comes around.
Also, please allow your users to configure the polling period, including an option for "do not poll".
For some reason this requestLocationUpdates() call is really blocking, at least in the phone brands that I tested. The way to fix it is to use one of its overloads. Please see this answer in another post.
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.
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 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.
Basically, I want to find out when another app starts, so I can adjust my app in a particular way. For example, if the user starts the GMail app, my service can detect that and react.
My current idea is to check 'topActivity' for each element in getRunningTasks() from the ActivityManager class. But that sounds quite expensive, since it needs to be constantly checking in the background (and I'm quite fussy about the CPU usage of my app).
If anyone knows a better way, I'd love to hear it!
There is not way of doing that the "nice way" meaning a BroadcastListener since applaunch don't get broadcasted. The only way is to Start a service that checks the getRunningTasks()
(For not to load the CPU too much you can also check what app is to the foreground. I'll search for the code if you are interested on that technic)
Have the service checking at interval of 5-10 seconds. Also shut down the service at SCREEN_OFF and restart it at SCREEN_ON.
Sorry but there is no other way for that.
I am developing an android app which fetches/uploads data from/to the web service every n minutes. This upload/download is only done when the app is running. But this might change in future.
I dont update the UI when the new data is downloaded. The UI is only updated if the user is on the current screen(app have multiple activities)
My question is what is the best approach to this problem.
I dont think service is the right approach as it sounds like an overkill(in the present scenario). AlarmManager could be an option.
Running threads inside a service be an option ..something like this .
Any pointers/suggestions would be great.
Thanks
I am using AsyncTask in my activity to ask .net web service some information and it works and easy to use.
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
Well, in this case, since the app would already be running during the time, either would work great, but a service can be called from anywhere within the application so this is where I would use the service over the thread.
If you want to create the thread to only be used in lets say Main.java, then thread would work fine, these are the only things that I can see really making ANY difference at all, they're really pretty close, and in this case neither gives a distinct "correct" answer, but I would choose Service
I think all approaches you noted would work ok. Personally I'd go with this:
Use AlarmManager to wake download service. Start it when Activity is shown, stop it when activity hidden.
Download service should be short lived: start it to do the upload/download and then shut it down.
If download service does get some new data, it sends a Broadcast which Activity listens to.
Just broadcast a message after your upload/download is done, and then have a receiver start the service and then have that service stop itself. And you are done.
This should be done if you dont plan on polling the server for new information or anything. Primarily this kind of approach would be for onetime update, interpret, finish. And wait until the next update. Which primarily for most cases is streaming.. but depends on what you are getting.