Getting user's daily route in Android? - java

I need to get the route a user goes through every day. I thought of checking the user's location every few minutes for 2-3 days and then analyze the information and get the origin and destination the user's going from and to every day.
The question is how will it affect battery life and is there a better way to do it. Are there any Android services that save the user's location history that I can access for this information?

You can register a broadcast receiver from the location manager to send you a broad cast if the location has actually changed. While I was working on something similar myself I found this blog post to be very helpful http://android-developers.blogspot.nl/2011/06/deep-dive-into-location.html

Related

So am building an app and it has this feature that i dont know exactly how to go about it

I hope this makes a bit sense, basically, I have this feature in my app for tracking calories which consists of having this page that only appears the first time you use the feature and it asks you to add personal details (so it can make the right calculations), after that you get faced with a simple page that tracks your nutrition with a button for the user to insert the meals he has eaten, this page has to save the inserted data (via firebase) and then restart from 0 each and every day.
my first problem is I don't know how I make the page that only appears one time to save personal data(to be more precise I don't know how to make only appears the first time). and the second problem is how do I make the app automatically sends the given data at the end of each day?
interface in normal state, interface when adding the meals
hopefully, this 2 images will help you get a better grasp of what am trying to explain
don't worry am not looking for someone to straight up solve it all for me, I just need some orientation about what type of things/functions I need to do to solve these 2 problems
While #Narendra_Nath's answer might work, please note that is not a bulletproof solution. Why? Because a SharedPreferences doesn't persist across app uninstalls. This means that your user can install and uninstall the app and see the page as much as they want. So if you indeed want a user to see a screen only once, then you should consider storing that data in a database. Please note that SQLite isn't also a solution because when a user uninstalls the app, everything that is stored locally is wiped out. So what's the solution?
The best way to solve this would be to store the data in the cloud, either in Cloud Firestore or in the Realtime Database. So you can set a boolean variable and always check against it.
If you however intend to implement Firebase Authentication, then another solution would be to display the screen when your users are authenticated for the first time. So even if they will try to sign in on another device, install and uninstall the app, they won't be able to see the screen again.
Regarding the second problem, you should consider using Cloud Function for Firebase. It's the most elegant solution. If you want to somehow schedule an operation, then you should consider using Cloud Scheduler, as explained in my answer in the following post:
Is it not possible to have a code in the background who will be called every 24h?
Make the page that only appears one-time -> store a value in the shared preferences "isInfoShownToUser -> false" then do a check when the app starts to check if this value is false or true. If it is false show the "take the info" page .. then turn the value to false in the shared preferences.
How do I make the app automatically send data -> Use a Workmanager implementation to send data to the server (Firebase) at a particular time ..
Or use a implementation like the first one which uploads the data to the server just once everyday

How do I prevent my android app from losing user data when it is inactive for a long time?

I am helping with an android app that involves keeping track of how much a person travels, and adding points for that person depending on the distance travelled. This means having to keep Data or internet access on the whole time.
The problem I am having with the app is that when the phone device is sleeping or the app is inactive for a long time, it will lose all the user information (username, e-mail, and point information). Even if I make the device use the method to track the person travelling, it will still lose the information after some time. Thus, it won't allow me to save the distance travelled data to the user.
I kept track of what causes this, and found that over time, the memory for the app would stop running, causing the user information to turn into null. I have tried finding solutions online, but the android developer website doesn't give any clear solutions. The closest I could find is this page about direct boot mode: https://developer.android.com/training/articles/direct-boot
I read over the page, but for some reason the application gets an error saying it can't run the code:
Context deviceProtected = appContext.createDeviceProtectedStorageContext();
The whole page just doesn't do a good job explaining how I'm supposed to use this code, or where I'm supposed to run certain code.
Does anybody know a better solution to my problem, or know how to properly use the Context class and Direct Boot to prevent app memory from being lost?
TL;DR How do I prevent my app from losing user/profile data, and keep certain methods/services running when the app is inactive for a long time?

Google Maps MVP Android

I'm using Google Maps on my app so I'd like to know a different way to do my goal. My goal is every time user joins a street it should call an api saying hey I'm in, so my question is, what's better use geofencing with this or shall I create a service that makes a call every 5 seconds to the server sending the current position?
It depends on how optimised app you want.
You can call geofencing every few secs, you can also make call to server to send current position.
You can also configure GPS to call only when user moved some distance, then for example check using geofencing if street was changed or send it to server. Some tweaks and testing will be required, to limit the calls to server and limit the battery drain

Android showing users on Map

On iOS I created an application where a user can share his location and other users could see him on the MapView.
I did this by getting the location with a location manager, whenever it changed by x meters, and then wrote the lat/long with a device id to a database using a PHP script. The other user could then press a button to show the locations on a map by retrieving this data from the database.
Now here is my question. First I think this is the way to make a application like this. If you think there is a more efficient way please let me know.
What is the best method to update this data for showing users on the map?
For my iOS app I used a timer which would get the lat/long from the database every 30s or so, but I dont believe this is the most efficient or best method. The app would have to load the data every second even if another user has not changed his position.
Could you also push me in the right direction on how to animate the pin from the old location to the new location?
Thanks in advance.
Because there are no answers I used old method which is using a timer to update the latt / long in my database. Works ok btw but was wondering if there is a better way.

Since when is the phone charging/discharging

I wanted to learn more about the Android Services / Broadcasts, so I started a simple project, to create a battery monitoring app. It turned out pretty good, I'm using it for a few days now, but I want to add a new function: to show since when is the phone charging/discharging.
First I thought that I would create two static fields in my BoradcastReciever extension class, where I get and publish the data about the battery, one for the actual state (charging/discharging), and one for the time, when the change in state happened. This way, I could just subtract from the current time the last change, and know exactly since when is the phone charging/discharging.
But there is a problem with this solution: It won't show the correct data at first, when a user starts the app. I wouldn't make a big deal of it, but I saw that Android tracks this data somewhere, because inside my phone settings I found this information, so why take the hard way.
So my question is: is there an easy way to get from the Android system the date/time (no matter what format) of the last charging state change?
I looked at the BatteryManager reference but there are no constants named after what I seek, and which I could use, to get the information from the Intent of my receiver.
The Android OS tracks the connect/disconnect of a power source, but does not make this data accessible to apps. You have to record this all yourself, using intent filters.
The two intent filters to use are android.intent.action.ACTION_POWER_CONNECTED and android.intent.action.ACTION_POWER_DISCONNECTED; with these, you can monitor when the power source is connected and disconnected.
You can find information about this process explained incredibly clearly here. Another blog describing the process can be found here.

Categories

Resources