Android NETWORK_PROVIDER location - android

I'm having little problem with getting my current location using NETWORK_PROVIDDER.
My code looks like this:
LocationManager lMgr = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
boolean isNetworkProviderEnabled = lMgr
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location location = lMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
// deal somehow with last (very likely outdated location)
}
if (isNetworkProviderEnabled) {
lMgr.requestLocationUpdates(provider, 0, 0, locationListener);
}
Of course I have MyLocationListener that deals with location changes and ALL REQUIRED PERMISSIONS ARE ADDED TO MANIFEST
And the problem is that on some phones this code works like charm, but one others, the "requestLocationUpdates" does totally nothing. Of course on those problematic phones, when I open Google Maps application, my current location appears immediately. So my question is (I believe that people from Google should answer this): how this is done that Google Maps retrieves current location immediately, and other apps don't? Is my code wrong? Of course I have seen code like this in many stackoverflow questions. If anyone wish to know, this kind of problem appears on some Samsung Galaxy Nexus S phones
In my application GPS is not used to save battery power, but location services are enabled.

I have recently faced the same issue, LocationManager is buggy and not implemented completely on Samsung phones. They also consume a lot of power. Use LocationClient to solve both these issues. LocationClient uses all means available to get you the last location, so its the super set of all your providers and sensors.
I faced issues with the Samsung Y model phones, I wonder which phones you are talking about. To fix the problem with Samsung phones, I kickstarted the GPS on the phone with
HomeScreen.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(final Location location) {
}
});
And then called the LocationManager.getLastKnownLocation OR the new locationClient.getLastLocation api.
First, just add the above lines over your getLastKnownLocation. Things should magically start workings
Migrate to LocationClient when you have more time and test again. Use the magic above to get it working if it fails.

Sometimes device does not receive any location updates and GPS_PROVIDER and NETWORK_PROVIDER returns null.
Try restarting your device, this trick helped me. My code was perfectly fine but providers were returning null.

Related

android :how to localise only with wifi or mobile network by code without using popup security menu

j know that since 4.0 it s impossible to trigger programmatically gps
but besides that there are three possibilities to localise
1) gps and wifi and mobile network (all together)
2) only wifi and mobile network
3) only gps
is there some possible code to get through the second one
so let's be clear j don't want to trigger wifi. that i know
j want to trigger localisation by whatever wifi (and)or mobile network without using gps
j tried to implement some object like skyhook' WPSPeriodicLocationCallback or WPSLocationCallback but
it doesn 't work without triggering the official android security menu
so what j want is getting through the positionning system only with wifi or internet connection and that by code
avast antitheft does that giving back some information with more or less accuracy .
i would like to reproduce the same
thanks in advance
You can get locating the position using the LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.
http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.
After you get done with most of the code in OnCreate(), add this:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
You could also have your activity implement the LocationListener class and thus implement onLocationChanged() in your activity or you can use this tutorial to get lat and lang.
Copypasted from this answer.

LocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) always returns NULL on Galaxy S7 (ONLY)

Just received a Galaxy S7 (Edge) running Marshmallow (6.0.1) and find that it has an issue with my app that uses android.permission.ACCESS_COARSE_LOCATION and targets Sdk Version 22 (Lollipop). When I call locationManager.getLastKnownLocation() it always returns NULL. Here's what I'm running:
public Location getLastKnownLocationObject(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// Location is always null on S7 (only)
Log.i(TAG, ">>> getLastKnownLocationObject() getLastKnownLocation: " + location);
return location;
}
} catch (Exception e) {
Log.e(TAG, ">>> getLastKnownLocationObject() exception", e);
}
return null;
}
This same code works fine on every other device I've tried: Galaxy S5 (5.0.1), Nexus 7 (5.0.1), Nexus 9 (6.0.1), and Samsung Tab3 (4.4.2)
Note that if I change the manifest to use ACCESS_FINE_LOCATION, the above code works fine on every device. Unfortunately, management won't allow me to change permissions at this time.
In a few answers I've seen here on SO, it's suggested to call the following prior to doing the getLastKnownLocation(), but that didn't have any effect.
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(final Location location) {
// NEVER CALLED on Samsung S7 for LocationManager.NETWORK_PROVIDER
}
});
So at the moment, I feel this is an issue exclusive to the new Samsung Galaxy S7.
From my experience with Samsung devices. If you reboot the phone and you DON'T enable GPS you will not get a location. If you enable GPS as some point - maybe by running Google Maps, then you will get a location. Samsung does not return a location for GPS, Network or Passive providers unless it has gotten a location during the devices power on cycle.
None of the calls I have made in code ever get it to kick in and activate and get a location. They work fine on other devices. Samsung does not seem to cache this information for very long either.
Well, you are calling isProviderEnabled() so at least that is covered and using the network location should be enabled. However the exact wording of the documentation is:
"If the user has enabled this provider in the Settings menu, true is
returned otherwise false is returned"
So it's just about the provider being "enabled" as in not being turned off by the user.
Does the problematic phone (the S7) have a SIM card inside? It's possible that on that specific device the network based positioning requires a SIM card or internet connection.
Referring to the the documentation you could check what these return:
locationManager.getProvider(LocationManager.NETWORK_PROVIDER).requiresCell();
locationManager.getProvider(LocationManager.NETWORK_PROVIDER).requiresNetwork();
And of course just calling getLastKnownLocation() will return null if the location is unknown. And it's unknown if no application (yours or some other) has specifically requested the device to determine its location. So the suggestion to call requestLocationUpdates() is the correct advice. But it may take a while to determine the location so calling getLastKnownLocation() right after probably still returns null.
And even if it returns something, it might be very old data that's not even valid anymore. So why not just subscribe to receive location updates? That's the way it's intended. You are using network based positioning so it won't (at all/significantly) affect power consumption and the updates won't come too often if you specify time interval and distance limits that suit your needs.

How to prevent other user to change gps location and Location updates called every small interval (say 3 seconds)

I've seen some application like gpsspoofer and fake gps apps that set location to spoof wrong location but my app to get right location please give suggestions.
My another problem
Loc.requestLocationUpdates(Provider.get(i), 600000, 1000, new LocationListener() {
#Override
public void onStatusChanged(String provider, int status,Bundle extras) {
/// some thing
}
#Override
public void onProviderEnabled(String provider) {
/// some thing
}
#Override
public void onProviderDisabled(String provider) {
/// some thing
}
#Override
public void onLocationChanged(Location location) {
/// some thing
}
});
}
My question is if i have already set location updates for 600000
miliseconds then why onLocationChanged(Location location) is
called every small interval which is less than both 1000 and
6000000
As far as i know it is possible.
Gps can be retrieved from two things.
GPS from the phone itself, this one can be spoofed by someone without root acces by using mocklocation. and someone with root and the right privileges without mocklocation turned on.
Also the location of the user can be calculated using the wifi (wps).
I don't know if there is any way to simply fake the wps location, but if you combine these two, and these are not the same you can know if someone is faking the gps.
Simple answer: you cannot. Your app gets the GPS location from the system. Those apps work in such a way that they make the system return you spoofed/incorrect values. If you're running on a rooted phone and have root privs, then you can check whether GPS calls are being intercepted and do something about it. Otherwise you're out of luck.
Reverse question: How can you detect whether there is a GPS simulation of some kind? http://gpscreations.com/Products_GPS_SIM14.html That is to say, you can't ever be sure. Even for software solutions, if you're checking, the apps may have expected this and have anti-checking mechanisms; so you need to check for those; this, too, may have been expected etc. - turtles all the way down :-(

Android onLocationUpdate not called with GPS_PROVIDER

Hi
I have an Android service using the location manager :
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.i("service","start with GPS"); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);
}
Then the location listener :
private LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location aLocation) {
Log.i("location listener", "location updated");
}
... other methods ....
}
In the manifest, the authorization for ACCESS_FINE_LOCATION is set.
Everything works ok both in simulator and phone ( Xperia Android 1.6 to 2.2 ) for NETWORK_PROVIDER. It works fine in simulator for GPS_PROVIDER. But when I try to use the GPS in the devices, the location listener is never called. I can see in the DDMS that the GPS is actually working and retrieve locations, but it never calls the listener methods.
There is a strange message though :
WARN/libloc_api(1173): loc_eng_report_position: ignore position report when session status = 1
I can not see what I am missing. Any idea ? Thanks.
It takes a while to get a location fix. The time to first fix de(TTFF) depends on a lot of factors, like number of visible GPS satellites, signal to noise ratio, the GPS chipset etc ...
Here is a nice article that will help you get the best out of GPS on android phones.

LocationListener works on emulator, not on phone

I'm having trouble getting a LocationListener to call the onLocationChanged() callback on my phone. When I run my code in the emulator, it works fine, the callback is called each time I do a geo fix.
When I run the application on my phone, nothing at all happens. The callback is never called. I have location enabled by both GPS and by Wireless in my settings. The application has all of the uses-permissions for location permissions.
Also, when I call getLastKnownLocation() on a LocationManager object, my application crashes. (Still, only on my stupid phone). Even if I try to catch an exception that's causing it to crash, it still just crashes, so I can't even get any information on what is causing it to crash. This is extremely frustrating.
LocationManager.getBestProvider() is returning GPS, and when I open google maps it finds my location in no time at all. What the heck is going on here? Is there some way I can figure out why it's crashing on my phone?
private void setupLocListener(){
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setSpeedRequired(false);
c.setCostAllowed(false);
lm.requestLocationUpdates(lm.getBestProvider(c,true), 0, 0, new LocationListener() {
#Override
public void onLocationChanged(Location arg0) {
map.setLocation(arg0);
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { }
});
}
onLocationChanged() wont fire until you actually start receiving GPS coordinates.
By that I mean the chip has to warm up for about a minute or so from my experience before you start receiving data from it.
I usually start some other application and wait for it to prove that the GPS chip has warmed up before I go testing any of my GPS apps.
I know that you mentioned that it works properly in Google Maps but have you tried clearing your memory and restarting your application straight away afterwards?
Also getLastKnownLocation() is always null until you start receiving coords.
The Location framework pushes coordinates to your callback, when they become available. Depending on weather, etc. you may not get a "fix" initially. You should see the "GPS" indicator on the status bar when your listener is successfully registered.
getLastKnownPosition() works just fine (it may return null); and Google Maps uses that, while it is waiting for an initial fix from the location provider.
You may also want to see what other providers are available, e.g. cell-tower data, and attempt to obtain data from those (i.e. LKP), either instead of, or until, your "preferred" provider starts pushing data.
Also, don't assume any particular service exists, e.g. LocationManager (Context.getSystemService() can return null), or any suitable provider exists, (getBestProvider() can return null). Your code will fail as-is on the right device with the right settings. If the documentation says null you must check for it, or users will be uninstalling it because it FC's all over the place.

Categories

Resources