I want to practise my Android development skills by creating an app wich sets 2 locations. To make it more interesting, I only want to set them while I am driving / arrived. The first, startLocation is created when the user is faster than 19km/h. I get that at the onLocationChanged Event with FusedLocationApi from google.
But obviously, there is the question when to create the endLocation. My solution for that would be to run a handler if the speed is less then 19km/h and wait 2 minutes. If the speed doesnt get faster then 19km/h within 2 minutes the endLocation get set.
Having said that I would like to hear some other solutions for that problem.
Thank you
That's the solution I used when I wrote a similar app 5 years ago. Since then Google came out with activity detection, which determines if a user is walking, biking, or driving. I'd just use that now.
Solution with handler that waits for 2 minutes is not the best, because
by that time your activity might be destroyed.
You can schedule task using Alarm Manager
Or if you are using API 21+ the best option is enter link description here
Related
I know that similar questions have been posted in the past and the most current solution I have found is to use a JobScheduler + wakelock + Foreground Service as explained for example in this excellent article by Roberto Huertas (https://robertohuertas.com/2019/06/29/android_foreground_services/).
However my doubt is to know if there is a limit for this method. Does it really work that well? What if the App stays in the background for days or even weeks, will it still work?
If the answer to these last questions is no, is it possible to keep a background service on Android > 10 that can keep running for days without stopping?
EDIT 1:
I'm trying to create a real time GPS tracking app (In this case I'm using Firebase). The company and the users who use it, give their consent to be tracked all day long during their activity. This tracking can be stopped if the user disables the option inside the APP.
I have managed to keep the service running in the background using various techniques, but after a few hours Android kills it.
No, it's not. It's actually less possible than ever. Background services are now limited to 2 minutes after you exit the foreground. Foreground services will be kept around for a while, but they won't stick around forever.
The correct answer on Android is to find a way NOT to need a service running at all times. This is almost always possible, but methods differ depending on what you actually need to do, which you haven't given us any info on.
I am trying to introduce auto-save functionality on one of my Android applications. The idea is that as the user inputs first name, last name and after a fixed interval I would like to save that information to the server before the user hits Next button. The final goal is to have something similar to the draft option in the Gmail app where your email information is automatically saved. So, if there is a timer that runs every 10 seconds, I will pass the information on the screen to the ViewModel and let it deal with the logic of saving the data to the server.
A couple of options I have explored are.
Execute recurring code with a specified interval using Handler.
PeriodicWorkRequest -- however this option has a minimum interval of 15 minutes which is a little too much for my use case.
AlarmManager -- This option runs even if your application is not currently running, In my opinion, this option can be an overkill.
I wanted to know if there are best practices/blogs around this and if anyone I on the wrong path or potential red flags with this approach.
you can make countdown for 10 second, when countdown is down save the data and call the countdown again.
when your activity is destroyed, so stop the countdown
Using google maps api to create a route while the app is open.
right now, I am updating every second. is this fine? how often does the google maps app update for example? Does it kill the battery/hog the CPU if I have the location updated every 1 second?
thank you
Once a second is fine.
You cannot save battery by updating once in 5 seconds.
To save battery you have to request in a much lower frequence, like once in 20 minutes.
It depends of your application how accurate the tracking must be and how often you need a location.
I think it depends on how precise your app should be. It is true, that having a short delay drains battery, but if you want to track user's movement precisely, you have no choice. Try to approximately check GPS interval of some similar tracking apps, but I think that many of them just keep GPS turned on.
Play with it and you will find the ideal configuration for your desired app. :-)
I've searched through a number of the questions and answers in regards to the LocPoller from Commonsware and haven't found the answer to my problem. I have tried using the demo of the latest from github and I'm having issues. First I must admit that I modified the PERIOD in the demo so the alarm fires every 15 seconds. I realize this will be a large drain on the batter and it's just for testing. I would expect that the LocationReceiver's onReceive() would be called ever 15 seconds but that isn't happening.
Debugging things I see that the alarm is triggering and everything seems good in that it's getting to the PollerThread and calling sendBroadcast. So I assume I have something setup wrong, but I'm not sure what. Other than changing the PERIOD in the demo everything else is as is. I've tried running this on a device and emulator (Level 8). Any help in debugging this would be appreciated.
In the end the functionality I'm looking for is to have the ability to keep track of a phones location so at any point my app can get the current location. I saw this in my research and though it fit the bill perfect. If someone has a better idea of different approach I'd love to hear it.
Thanks,
Rindress
In the end the functionality I'm looking for is to have the ability to keep track of a phones location so at any point my app can get the current location. I saw this in my research and though it fit the bill perfect.
No, it is not.
If someone has a better idea of different approach I'd love to hear it.
Step #1: Call requestLocationUpdates() when you want to start receiving location updates in your app
Step #2: Use the location fixes delivered to your LocationListener, or call getLastKnownLocation(), as needed
Step #3: Call removeUpdates() when you no longer want to receive location updates in your app
My app periodically refresh data by pointing the api. Right know this process is every 3 minutes. Thats mean that every 3 minutes app, points to api requests and calculating data.
What is the best time value between refreshing points? Somewhere i ready that good is every 30 minutes.
I focusing right now on battery life and looking for the best solution.
Thanks.
Exact answer for this question depends entirely on how important is "refresh" for your app to work stably. I haven't checked the c2dm approach.
Few steps can be followed to improve performance and battery
1) Stop your periodic refresh when application is in background
2) Follow the design tips thats been given here.
3) Its better to give user a choice regarding Refresh timer with 10, 20 , 30 minutes as choice in your app settings (optional).
You can use Google's C2DM to notify the device of new information instead of polling for it. Yes 3 minutes is too soon, use 30.