I have an application that is required to log the user's location every couple of seconds, the entire time it is on.
On does not mean in the foreground,or that even the Android's screen is on.
What is the suggested way to accomplish it?
I've heard of background and foreground services, and also saw something about Jobs. I also saw this was possible with a WakeLock.
I am not sure what is the best method of choice.
The need to conserve Battery life is of course an issue.
You should use Service, you can choose that the service wil keep running even after your app was closed.
YET if the device is low on resources it might close your service.
you can use FOREGROUND service if you want to minimize the chance android will close your service.
inside the service use LocationManager / FusedLocation to get the location every X time.
you set it up with LocationListener so everytime it set onLocationChanged you upload to firebase.
Useful links:
Make your app loaction aware
Services
Related
I need to keep rough track of a users position, but not really in real time. It's sufficient to handle the location updates when the app is started. However, I still need to know where the user was when the app wasn't running.
Is there a way to get the location history in an app?
I don't really want to have a service just polling last known location all the time since that would be a waste of battery power.
However, I still need to know where the user was when the app wasn't running.
That is not possible.
I don't really want to have a service just polling last known location all the time since that would be a waste of battery power.
Then eliminate your requirement for location history. You only get the locations that you request.
Rough Track can mean you get location of the user (lastKnown or Fresh) after every n-hours. Doing this will not require a service, simply a recurring alarm and receiver will do. In the onReceive method of receiver, you can manage a stack of locations in your app.
You will have to reset the alarm though when the device re-boots. I guess this is an add-on, rest should work fine.
you can not get current location without running your app...
the second way is to make background service ..wich you don't wan't to make...
The other way is to run background service using Alarammanager whenever you want after getting location you can stop the service...like you can make call every hour or 2 times per day...
Like many others, I want to have an exit button that will turn off the GPS when the user leaves the app to conserve battery life. Many discussions are posted basically saying this cannot be done but I have an application that does it so continue to try and find a way. The developer of that application told me that he runs the internal GPS in a service that he wrote and that his exit key kills that service and vola, the GPS indicator goes off instantly.
My app is very time sensitive and I would like to use the equivalent of onLocationChanged that I use now when using the LocationListener part of the system service but have the system service inside a service I write so that I can kill it.
My question then is can I put the system GPS service inside a local service without adding any significant delay, without me having to poll the local service for updates. Any help on how I can do this would be appreciates as this is my first Android app and although I have written tons of C and PHP code, this is new territory and a bit strange.
Like many others, I want to have an exit button that will turn off the GPS
You cannot "turn off the GPS". You can tell Android that your app no longer needs GPS (e.g., you no longer need GPS updates). Android will then determine whether or not to power down the GPS radio, depending on what else might be trying to use GPS at the moment.
when the user leaves the app to conserve battery life.
You do not need a button for this. Simple apps, where only one activity needs GPS access, can simply request location updates in onResume() and remove them in onPause(). More complex apps might request location updates on the first onResume() that needs them, then remove those updates in onUserLeaveHint(), or do a reference-count of resume/pause operations to determine when to remove updates.
There may be scenarios where you really do want the user to have to explicitly say "stop using GPS" by clicking a button, but if you can avoid it, please do so.
The developer of that application told me that he runs the internal GPS in a service that he wrote and that his exit key kills that service and vola, the GPS indicator goes off instantly.
Somehow the developer needs to determine when to start and stop this service. Rather than starting and stopping the service, they could request and remove the location updates, and have the same effect, while consuming less heap space and making it less likely that the user will attack you with task killers and the Force Stop button.
IMHO, the only reason to use a service with location updates is because you specifically want to consume location information in the background with no activities around.
I have to create an app which runs a service in background and continually update the current location on the server or we can say i have to send my current location at some points. The solutions i have think are following. please look at that and give me some idea.
Running an service in background with boot-starter.
Implementation of alarm manager so service can be started automatically if android kills it.
If my current location is in the radius of the 500 from my points than start to update on server.
and other logic which i have think to prevent continues server update.
I have research on it and found it consumes much battery of phone. So please suggest me how can i optimized it.
There are some ways that can keep your Battery work for a long time though you are using GPS Application.
Turn the GPS function off when not in use, then on when you need a
location.
There are some more ways that you can keep track that GPS doesn't drain much Battery, just read this BLog to get some more idea about it.
I'm looking for the best way to continually monitor the location of an Android device, while using the lowest amount of battery power. My project is basically a phone tracking service, with phone call / sms logging, and location tracking. There is a web interface, which would eventually allow a person to change settings (such as location update interval, so if it was stolen they could track it more effectively), but as standard it should update the location every 30-60 minutes.
I was thinking of using a Service, but would it be better to start a service, request location updates, and then finish? Or some other method?
Cheers
but would it be better to start a service, request location updates, and then finish?
Yes, simply so you do not need to keep a service around all the time. However, it's a bit more complicated than that:
you might never get a fix (e.g., the phone is underground and cannot get GPS signals)
you need to keep the service around until you get a fix or until some timeout occurs
you have to worry about the device falling asleep while all this is going on
I have a LocationPoller component that handles most of that.
I am new to Android, and I need some advices for start up.
I want to build an application, which will show up, when the user gets into some hot situation.
By hot situation I mean:
the GPS/cell coordinates are in known zone;
known Bluetooth device detected;
known Wi-Fi network detected;
weather info has change;
I see something running in background and when one of the clauses hit, it will trigger and open the app.
How to get started?
How do I make sure my app won't be shut down?
As I read somewhere that Android OS will terminate apps if memory out occurs or consumes too much, and my app would consume a lot, making repeated measures/checks to see if situation changed.
Regards,
Pentium10
You need to use a Service for the part of your application that runs in the background.
You might find the Application Fundamentals document in the Android Developer Documentation helpful. It says this about Services:
A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it.
In you case you might find the LocationManager Service helpful. It is a system Service which will you can use to notify your application based on GPS position.
However, I think you'll have to write your own Services to monitor Wi-fi, Bluetooth and weather.
You can use the AlarmManager Service to get your Service to perform particular tasks at certain intervals.
It depends on how & where you want to deploy your application. In my experience it boils down to
you create an application for a specific use case where battery drain matters less than accurate results (showcase situations, prototyping, ...)
you want to distribute the application to users.
In case 1) just create one service that aggressively polls the sensors / web services. Use the AlarmManager to send a REFRESH intent (AlarmService.setRepeating(...) ).
That REFRESH intent will restart the synchronization service everytime, even if it was killed by the system. onStart() will be called everytime the REFRESH intent is emitted. You can do heavyweight setup logic in onCreate() as this will be called everytime the service is created after it was destroyed. WARNING: This will possibly drain the battery very quickly.
In case 2) I would create several services and let the user configure different polling intervals for each service to limit battery drain. I can see for example that bluetooth should be polled more regulary than GPS as it is more likely that a bluetooth device suddenly appears than a user moving extremely fast.
Weather sounds extremely expensive (network lookup, possibly triggering a network connection!)
Please do not try to be too persistent with your app in case 2). It usually makes a lot of sense for a phone to kill memory / power draining services.