Get Location Updates in Background Service - Android Studio - android

after researching the Android Location FusedLocationProviderClient I am very confused about what you should and should not do!
The use case is: The App holds different locations with latitude and longitude. While the user is walking around the app (with the phone in the pocket, screen off) needs to track if he is close to one of the locations and plays a short beep when closer than a defined range.
Would the Geofence option the best solution? These locations will also change during runtime and periodically - they might also be more than 100. Any suggestion?
I am also thinking about a background service to handle all this when the device moves. This would be more flexible and allows for the reload of the locations as well. Any thoughts?
Thanks!!

You don't actually need to build a background service to monitor if a user is close to a location. There is already a geofence provided by Google. It uses fusedlocation provider internally.
Please refer this
You can combine this with a FusedLocationProviderClient and subscribe to location updates. This way you'll get location updates as well.

Related

Location with GPS_PROVIDER

Sorry, I'm a beginner in android.
I have to save when a car parks using a service.
To do this I'm using LocationManager with GPS_PROVIDER to monitor the speed of the car.
I am using requestLocationUpdates but when the machine stops onLocationChanged it is no longer called so I can not get the speed, understand that it is stationary and save the parking space.
How could I do?
If you want continued updates while stopped you need to ensure 'minDistance' is not preventing updates. It is passed to your 'requestLocationUpdates' call. Try setting the value to 0. However this will also have a negative impact on battery life. I would also suggest looking into the location APIs provided by Google Play Services instead of using the LocationManager.

Monitoring android GPS location

I am building an app that will monitor where a user is using GPS and alert him if he is within a certain distance of a particular place. I would provide the app with a list of the places and their latitude and longitude and the app would check against this list. Once the user has launched the app to start the location monitoring process, then most of the time he does not need to know anything about it and will be using other apps, making phone calls, or whatever. A user interface will only be displayed when the app finds a match with a location in the list.
I guess that this means that the location monitoring should run in background mode, but I am fairly new to android and I don't really understand about background processes or about location monitoring. Can anyone point me to a good tutorial or information?
to building this app that will monitor where a user is using GPS and alert him if he is within a certain distance, you will using service in background and using gps to get altitude and longitude and you will using a function to calculate a distance bitween a user location giving by gps and others location in the list and you can also start your Service on Boot Automatically.
good luck.

Getting GPS co-ordinates quicker programmatically without internet, but using network provider and mobile GPS

I have referred many questions on this topic, but it seems some of my requirements are missing:
I want to get the GPS location at ~0.0001 accuracy
Don't want to use internet; Though GSM/CDMA network is ok
Should be obtained programmatically when the app starts
Should be quicker, say within a minute (like iPhone, which probably works in Airplane mode as well!)
The code should work in most of the devices
The phone may switch-on/off anytime and travel any distance
Is there any way to get just the co-ordinates with above mentioned requirements? Any sample code snippet will be much appreciated.
Checking various apps like "Locate Me", "Maverick", which claim to show offline locations. But in various scenarios without internet, they don't give track the location (in India).
I have referred plenty of old/new questions inside/outside SO. Below are few:
Is it possible to have the gps location while offline with the nexus
7?
Does GPS require
Internet?
How to get current exact location in android without internet
connection?
[Note: I am asking this question on behalf of my Android team and will be happy to clarify the details if asked for. Should you feel that this post belongs to Android.Stackexchange, then kindly move it.]
1. I want to get the GPS location at ~0.0001 accuracy
You can listen only to GPS provider and discard a location when it doesn't have the minimun accuracy you want:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5, myLocationListener); // "myLocationListener" must be an object from a class that implements LocationListener
// "myLocationListener" implementation of LocationListener.onLocationChanged
public void onLocationChanged(Location location)
{
int MIN_ACCURACY = 5; // in metters
if ((!location.hasAccuracy()) || (location.getAccuracy() > MIN_ACCURACY))
{
// discard this location and keep listening to new location readings
}
else
{
// that's a good reading!
// do somethings you want... blah, blah, blah...
// stop updates when you get the location to preserve users' battery.
locationManager.removeUpdates(myLocationListener);
}
}
2. Don't want to use internet; Though GSM/CDMA network is ok
Yes, GPS works totally off-line. Android can make use of internet ONLY to update A-GPS data cache and provide faster reads in cold-start updates.
3. Should be obtained programmatically when the app starts
Then call item locationManager.requestLocationUpdates on mainActivity's onCreate event.
4. Should be quicker, say within a minute (like iPhone, which probably
works in Airplane mode as well!)
Keep in mind that iPhone works with "high quality hardware". Android can be run on crappy devices. So it'll depend of:
The device's hardware quality;
The number of satellites that are visible in the sky at that moment;
The age of almanac and ephemeris data on gps cache;
GPS can fail to read satellites because user is inside a building or something.
5. The code should work in most of the devices
What is the oldest Android's API you want it to run?
6. The phone may switch-on/off anytime and travel any distance
I didn't get it. What is your concern about this?
------------------------------------------------------------------------------
Update 1:
"...I would like to use Android 4.0 and above..."
I've tested GPS features from Android 2.3 to 5.0 devices. Everything runs pretty fine on all of them.
"...I have observed that 1 geo-coordinates based demo app which was working in other devices, din't work in my LGG3 with Android 5.0. Any idea on that?..."
Did you check GPS permissions on Android settings? (Maybe it's disabled) Or can be a hardware issue? Did you try in another similar device?
"...Suppose the GPS is showing correct location in New York,
I switch off the phone and then switch on after reaching to London,
then will it still show correct location (without internet)?..."
Sounds you're confusing things: reading a GPS location is one thing. Showing that location into a MAP is another different thing!
You don't need to be connected to the internet to do GPS location reading. But, if you want to show that location into a MAP, probably you're gonna need internet (to load map resources, etc.).
If you nedd to stay collecting GPS locations periodically (let's say, from 10 to 10 minutes), then it will be better to use AlarmManager to schedule a timer that will "finger" your app and say "hey, time to make a GPS reading!".
"...Also what is your opinion about the latest Fused API?..."
I've tested it and used it for a while, but I gave it up. It needs that Google Play Services be installed to work (not a problem, most users have it on their devices). But if you need ACCURACY (as I do), it will not work. It uses "fused sensors" (accelerometer, gps, wifi, compass, etc...) to try to get user location with minimum power possibile. Sometimes, it says you're 10 miles away from where you're really is. I couldn't make it work fine to keep the "path" where user has been. But it really saves battery.
Suggestion is to use Google Location Services
It takes the best possible and accurate location as accurate as it can at current moment. It automatically (with configuration of course) takes best current accuracy too - whatever is available GPS, network, internet, GSM/CDMA/LTE... It also cashes last known location so, basically, you know it every moment - the best what you can.
Of course you have to realize that each service provides its own accuracy and in it's own time. Look, for example, GPS Test App on Android and see how accuracy increases with time and used satellites.
Also Location Services is good for you because it simply provides coordinates - just as you asked and hides a lot of work to determine what real service to use based on time and accuracy. However, of course, if none of the services on your particular device and location can give you required accuracy then there is no way to get it. That's why services also provide accuracy measurement.
Here is another link
Use fused location API provided by Android SDK.Implement fused location in a Service and call it in your application MainActivity.

Android Location Updates get stuck

I am developing an android application wherein I need the user location updates pretty frequently. Say 2 times a minute.
Earlier I had been using Google Play Service's "Fused location service" but the location updates were not received as requested.
The location updates got stuck for sometime, the interval between updates jumped to 10min or so.Sometimes even if I put my priority to "PRIORITY_HIGH_ACCURACY" the same happened.
I then went back to the old "Location Manager" and when I used the "NETWORK_PROVIDER", I noticed that the location updates got stuck due to this provider. Also the GPS does not get activated immediately, it takes some time. I am trying to build my custom fused location provider. How can I efficiently switch between providers, without getting lags on location updates.
I want to know what are the best practices for getting location updates regularly, all the time, be it either NW, GPS or both. Like it should work for an application where location updates getting stuck cannot be afforded.
Battery drain is not an issue for me right now.I am aware of all the supporting docs that Google provides regarding location access.
Any help would be much appreciated.
Thankyou !
FusedLocationProvider really is the best option for obtaining locations at the moment, because it uses a lot more than just GPS or Network data to obtain location fixes. I have experienced issues regarding intervals being missed as well, but ultimately this is something down to luck depending on availability of GPS, Network, etc. etc.
My favourite usage of FusedLocationProvider so far is in conjunction with the AlarmManager class: Basically, the idea is to start location tracking at intervals specified by the Alarm Manager (this can be set to every 30 seconds such as in your case). Once the interval is hit, the location provider is to obtain a location fix as soon as possible (so in the setInterval method or whatever it's called, the parameter is a 0). This way, you can avoid having to wait another 30 seconds for a new location, instead having the location tracker attempt to provide a location as soon as possible after the interval specified by the Alarm Manager is hit.
By the way, when making custom location tracking wrappers, be careful of using the .getLastKnownLocation() method as it only uses cached locations - you could end up sending the same location to the user every 30 seconds.
Good luck!

Is it possible to make a location based app?

Would it be possible to make an Android application that works based on location, for example when you get to work the app might turn off WiFi or Bluetooth or change any other system settings in order to save battery or something?
The app would require the user to enter in locations where they want the app to begin working, like their address, and when the phone sees that they have arrived at this location it will start working. I want to do this but I don't know where to start.
I already have some android app development experience.
Yes absolutely this is possible. I have worked on approx three application with same concept.
You need to create a center point and define a radius for the same.
Then you need to calculate the distance of you current location with that particular center point and if your distance is less than or equal to radius, trigger to switch on the services you want.
In android you can use
LocationManager class
For collecting the data. If you want some more help I can provide the same too.
I've played around with a concept like this on the Windows Phone for a while, and I think this would work for Android too.
Basically, you'd use the GPS of the device to detect your location, and then compare this to the locations saved by the user. If the location comes within range of a saved setting, perform an action.
GPS usage however drains battery, so you'd have to experiment a bit with making this an efficient method.
Have you tried GeoFencing, its really cool. It lets you define a center point, and a radius. Once the user enters this radius you get callback or a pendingIntent. You can do what you want in your callback.
You can get addresses of your customers and build geofence around their locations.
Edit :
If you choose to not use GeoFenceing (for what ever reason), note that LocationManager is highly buggy and unstable on most mobile phones (Samsung).
Instead use LocationClient, which takes care of all the crap LocationManager leaves with us to deal with and provides location faster, lesser battery and customized accuracy.
Edit :
If anyone tries to convince you otherwise, check this out. A video from google developers and their talk about why LocationManager sucks, and why they knew and helped us figure out the value Sensors add to the entire solution.
Edit : Design of your solution
You need 2 geofences
1) Outer, you can hard code this to a imaginary 500 m from office. From then on you can start looking for wifizones
2) Once a wifizone is found, save the gps and the distance from his address point. That is your inner radius.
1 Geofence is defined by your install, 2nd geofence is defined by your runtime.
2nd Geofence and be used henceforth to disable GPS (only incase you decide to use LocationManager). If not you wont need this, LocationClient will do what it does best.

Categories

Resources