I want to track the user location at every 1 minute and store user location in database.
If the user location does not change for 30 min then i have to give the notification to user and if user does not close app at particular time.
e.g: 8.00 pm then also i have to give notification to user
The tracking should be in separate thread so that app can perform other task while location tracking is in progress.
I am totally stuck in this that how can i make separate thread for three things:
for location tracking
for 30 min reminder notification
for 8.00 pm reminder
if the notification is shown to user and if the location changes then notification should be removed and tracking should be done normally
I see no need to use threading for this. The Android documentation on the location strategy page defines how to get updates on the location, and the Countdown timer can be used to do your timing.
You need to use Service for this, and in service you can implement the location listener with with one minute interval, and for battery optimization you should you Google Location API. And you also use the Activity recognition for finding out the user movement.
Your battery life will suck if you check the location every minute.
Anyway, all of these tasks can be completed by using the AlarmManager. You'll set up a time for the next event and when it happens your code will run, even if your Activity is in the background. I'm not exactly sure what you mean by "close the app at 8 pm", the app doesn't need to be visible for these things to happen.
Related
There are some questions in my mind would like someone to answer and clarify:
I have background service that sends coordinates of location every second from background and updating location on another device like happens in driver customer model, what will happen to this app under background execution limit? And how will I survive this execution limit and after that what will be behaviour of my application? Will I have to start forground service for this?
Also what if I disable location permissions for this app frim settings while its working and sending coordinates, what will happen? Moreover can I notify user that location permission is disable and service is stoped?
Thanks
If you need frequent location updates (like for tracking exact path, the user was running) you definitely need a foreground service. Just add notification to your service and tell it to go foreground, displaying said notification:
startForeground(yourId, yourNotifcation)
If you need to know, when user happens to be in / outside some area, than Geofences are your best bet. Say, you want to know, whether he have left a train station. Or is nearby a certain shop.
If you need just infrequent (like every 15 - 20 minutes or more) updates of user location, than you can do this via WorkManager
I have implemented a foreground service to get GPS updates for longer runs, like 3 hours, when the app is in background. It is our business requirement. But the app stops getting updates after a few minutes. I see the "location icon" on top bar go disappear, even though my foreground service notification is still there.
I have also excluded my app from Battery Optimization list from the settings, so that Android doesn't kill my app. It helps to keep gps updates alive for about half an hour only, then again the location icon goes away and i stop getting gps updates.
What can I do to keep it always ON?
I am posting this answer based on my short comment and OP request
You can start that service by separate process by adding android:process=":FusedLocationService" in your Service component in manifest.xml.
I had done this and work in most of the device except some of the manufactures like Honor and other because they customized Android AOSP for better user experience
https://developer.android.com/about/versions/oreo/background-location-limits.html
Apart form this you can use JobScheduler to fetch location data after a particular interval (approx 15 mins)
You can build logic a/c to OS version as well.
And while fetching the location use FLP (FusedLocationProvider)
Hope this will help, But after all this depend on Mobile brand(like ONE PLUS) you will face the issues. You need to handle these scenario as well.
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...
Problem:
The user can choose how long and when start to track your locaton (Like today from 08:00 to 16:00). So during this interval my application will listen for the location every 5 minutes (this time can be changed, it's a user's preference).
What I'm thinking:
Making a service to be running and listenning for coming of the GPS or network locations during the time interval definned by the user.
Problem:
The service can be removed for running a long time?
I need to registers listeners to GPS and Network (even if they have not enabled) when starting the service or I need to register and remove Listener depending status of GPS and Network?
My service will continue running even if the user is not using the phone for a long time (like an hour) right?
You don't need to register a Service for this. You can use Mak Murphy's cwac-locpoll library.
This is a short description of the library taken from the page linked above. You'll also find code examples there.
You simply set up an AlarmManager alarm to contact LocationPoller on whatever frequency you wish, and it will handle all of the location work from there, sending you the results via a broadcast Intent. Your BroadcastReceiver can then use the location data as needed.
I would like to write an app on Android to upload my GPS location to an external website once every ~5 minutes. This needs to have as minimal an impact on battery life as possible, but it also needs to work without any user interaction. (Background: I'm competing in an Ironman triathlon which will take me about 14 hours to complete, and want to broadcast my location in near-real-time but without having to worry about fiddling with my phone.)
So my initial thought is to write a Service which uses LocationManager.requestLocationUpdates() with a minTime of 5 minutes, but will this actually wake the device up every 5 minutes for my service to do its job?
It sounds like I would also need to use AlarmManager.setInexactRepeating() to make sure my service is awake while it completes its task but how does that play with requestLocationUpdates()? Should I instead set minTime=0 on requestLocationUpdates() but then go back to sleep as soon as the next update is obtained?
Any general guidance on how to design this is greatly appreciated. I'm a competent Java programmer & will be using Google Maps on the server to plot my location, but am pretty new to Android development so I'm basically looking for a high-level plan on how to architect the client app.
Your service must be alive all the time you want to receive updates.
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29
You can tell how often you want to be informed of location change with minTime parameter. It does not however decrease battery consumption. GPS is enabled unless you use removeUpdates method no matter how often you want to receive updates.
You can use another approache:enable GPS using method above, read one value, use removeUpdates method, wait 5 minutes and all over again. Delay between enabling and retreiving a location can be between few seconds to few minutes.