Best way of setting mocklocaiton on Android - android

Apparently there are 2 ways of setting mock location in the Android ( currently playing with Android 11 )
One way using FusedLocationProviderClient:
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.setMockMode(true);
mFusedLocationClient.setMockLocation(createLocation(LAT, LNG, ACCURACY));
Another using LocationManager:
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
lm.addTestProvider(providerName, false, false, false, false, false,
true, true, 1, 1);
lm.setTestProviderEnabled(providerName, true);
Location mockLocation = createLocation(LAT, LNG, ACCURACY);
lm.setTestProviderLocation(providerName, mockLocation);
Wondering what is the right way.
For me second works fine, the first one in not ( still investigating )
But I wondering what is the RIGHT way?
Also wondering why there are 2 distinct ways ?

The difference is LocationManager vs LocationServices.
LocationManager is the built in Android location functionality. It provides access to location via GPS or network identification. Every device has this.
LocationServices is a Google Play Services functionality. It main value is the "fused" provider that uses network, slight GPS use, and other sources of data to provide a location. It tends to be more accurate than network with less battery power than GPS (but not as accurate/precise as GPS). Only devices whose OEMs pay Google for access to Google Play Services have this (this is mainly an issue in Asia and Africa).
Two different systems, two different ways of mocking location. Your top one works if you're getting locations via Google Play Services, the bottom if via LocationManager. Use the one matching the way you get locations normally.

Related

OnLocationChanged not fired on some devices

I'm aware that there's a lot of 'similar' questions, but none of them provided a useful answer.
I am trying to listen to location updates, both from network and GPS. The LocationListener initialization is pretty standard:
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 15000, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 0, locationListener);
I tested it on two devices, Samsung Galaxy S4 (SM-N910F) and LG G Flex 2 (H950). On the first device onLocationChanged is called as expected (only from 'network' provider), while the event is never fired on the second.
Both devices are set on 'High Accuracy' mode. I tried the second device on Wi-Fi as well as on 4G LTE connection. Since I use Google Maps API, I can clearly see the user marker moving on map, yet the event is never called.
Any idea why onLocationUpdated is never called on the second device?
Additionally, I read that GPS takes a very long time to fix. Is there any point in requesting updates from the GPS provider?
Thanks!
May I suggest you start using Google Play location APIs.
The Google Play services location APIs are preferred over the Android
framework location APIs (android.location) as a way of adding location
awareness to your app. If you are currently using the Android
framework location APIs, you are strongly encouraged to switch to the
Google Play services location APIs as soon as possible.
https://developer.android.com/training/location/index.html

Android location accuracy

I have the following code implemented to retrieve location values on Android:
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
On Android 2.3 and 4.1 this works great and returns updates at 180 samples per hour with a resolution down to 100m or less.
However, on Android 4.3 something has changed so this only provides updates at 5 samples per hour and a resolution at 2km. I haven't tested on later versions.
What has changed? Is it possible to return to the old accuracy levels again? I don't want to force GPS for power reasons. The app has ACCESS_COARSE_LOCATION permissions.
I have been testing this on a Samsung S3 if it makes a difference.
Use Google's LocationServices API instead:
https://developer.android.com/training/location/receive-location-updates.html
LocationManager.NETWORK_PROVIDER do not provide accurate location it provides you the location according to the cell tower you connected,sometimes it provides location with accuracy of 100m sometimes with 2-3km,if you do not want to get the location from gps due to power reasons i would suggest you to use fused location provides as this is an efficient and effective way to get the location on android and Google also recommends this,you can read more here in this example http://www.kpbird.com/2013/06/fused-location-provider-example.html

GPS works better with google maps than my App android

I have an app in android using GoogleMaps API.
When i use google maps, I active gps and it find me so fast.
When i use my app, it takes some seconds, and if I'm in my house, gps dont find me. WHY ?
A short summary of my code:
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyLocationListener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
7000,
5,
MyLocationListener
);
Thx for answers and sorry for my poor english
EDIT
Thx for answers ! the problem is that i dont want to use network provider. Wifi is exelent, and 3g have a 400 meters error jejeej.
I only want to know, why google maps, using GPS, find me in 1 second with perfect position Although I stay in a house.
Your answer is "use coarse locate". If google maps use network providers befor gps, its impossible he find me so fast. :(
GPS is using satelites, When you are indoor it can't get the signal from the satelites hence it can't find location (can't get a fix).
The GPS needs signals from at least 4 satelites to get a proper fix, that usually takes a few seconds, once you already have a fix you can maintaine it very easly. therfore if you are using google maps it will get you a fix faster.
To get a better understand on how GPS works, check this link
You might want to use different location approachs for getting location indoor (coarse location could be the answer). I would also consider using the fused location by Google play services, it gives you a wrap up on the location providers and get you location based on what avilable at the time (meaning GPS, coarse - wifi or network).
You should try using Coarse locates for a quick locate followed by a fine locate to have a more accurate location. The coarse locate is usually very fast, while the fine one takes more time.
Basically, the idea is to do multiple locates and overriding the less accurate ones by the newer and more accurate locates.
You can find more info on the Android dev doc: http://developer.android.com/guide/topics/location/strategies.html
EDIT: if Google Maps is able to find you so fast, it is because you are not moving and they use the knowledge of the last location. Please read the above link that gives a lot of useful information for Location strategies (and it works very well).

get coordinates without gps (android)

I'm developing a android app and I want to receive coordinates from a smartphone. Two questions:
1: GPS is enabled, but there is no signal, so I have no coordinates. How can I check, if GPS is sending coordinates or not?
2: If GPS is enabled and there is no signal, which other method can I use to get the coordinates from the smartphone?
Getting coordinates requires using the location services, which by default uses all manner to determine location, not just the GPS, but cellular signals as well. Here is a tutorial on using the location services in Android.
One word of advice - if you are going to use GPS and generally distribute your app, know that the GPS service is VERY battery intensive. It requires judicious coding to not drain your users battery. You have been warned!
1) fetch device coordinates using locationService.
mContext = getActivity();
mLocationManager = (LocationManager) mContext.getSystemService(Application.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(mProvider, 60000, 100, mAttLocationListener);
boolean gpsEnabled = mLocationManager.isProviderEnabled(mProvider);
2) You can still get rough estimates using location information stashed in wi-fi

Android Location Wifi doesnt work after rebooting

I am using this code to get the location provider and location.
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
mBestProvider = mLocationManager.getBestProvider(criteria, false);
mLocation = mLocationManager.getLastKnownLocation(mBestProvider);
If I turn off the GPS, the location is by network, but if I reboot the phone(so I loss the last position known) and with the 3G data connections off. So I am only using WIFI, I cant get any provider thus therefore any location. However google places app can locate me. I think it might be getting the lastknownlocation.But in that case my others applications should be able to get that location. Any idea whats happening?
The way you are calling this it will return all providers enabled or not because you are passing it false which is probably your intention but have you checked the return string?
mBestProvider = mLocationManager.getBestProvider(criteria, false);
You might be getting the gps provider, or you might be getting the network provider, I have learned not to trust the criteria mechanism because it seems to work differently per carrier and device (I have had some weird bugs reported because of this)
So I always ask specifically for gps and network providers and check last known for both, then use an algo to determine the best one to use.
The network provider can use cell or wifi hotspot/routers to determine location (google keeps a database of wifi information) so it's possible to get a fix with just wifi, not saying that is whats happening but it could be.
If that bears no fruit then it's possible that they are simply caching the last location update in preferences, some applications do that. To test the thesis, failing all of the above just leave the phone in that state and move to a very different location with the same properties if possible, should only take 2000 meters or so. If your app still reports null and places reports the old location you have your answer.
If places did report the newer location with wifi, and your app cannot (assuming you actually verify you are getting the network provider) then there is a chance they are using a private API via the Google Location Server (GLS) / MASF server via partial cell / wifi info but that's at the extreme end of the tin foil curve.

Categories

Resources