Is there a way to track gps location even if the app is running in the background?
Yes, create a Service and request location updates there.
This should point you to the right direction: http://niravranpara.blogspot.se/2013/04/android-get-current-gps-location.html
#Override
public void onLocationChanged(Location location) {
// here you get location updates
// the link I gave you doesn't tell u that, but it is obvious?
}
Related
I am developing an android application in which I need to get my current Location. I have successfully wrote the code and I am getting my current location using Google Play Service.
The problem is sometimes it gives me the location after a long time. I have noticed that it was only for first use of the app.
Any way to avoid this problem and get the current location fast? Is it related to the version of google play service in my code? (I am not using the last one in fact I am using version 9.8.0.)
As #tahsinRupam said, avoid using getLastLocation as it has a high tendency to return null. It also does not request a new location, so even if you get a location, it could be very old, and not reflect the current location. You might want to check the sample code in this thread: get the current location fast and once in android.
public void foo(Context context) {
// when you need location
// if inside activity context = this;
SingleShotLocationProvider.requestSingleUpdate(context,
new SingleShotLocationProvider.LocationCallback() {
#Override public void onNewLocationAvailable(GPSCoordinates location) {
Log.d("Location", "my location is " + location.toString());
}
});
}
You might want to verify the lat/long are actual values and not 0 or something. If I remember correctly this shouldn't throw an NPE but you might want to verify that.
Here's another SO post which might help:
What is the simplest and most robust way to get the user's current location on Android?
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.
I'm trying to get location manager to work in an Activity in my Android app but it keeps giving me the error "cannot resolve method requestLocationUpdates"
I'm putting this code in my OnCreate (found from other examples):
// 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);
I want to use my own "OnLocationChanged" method to update a map with the users location, but I can't figure out why I'm getting this error.
Any ideas why I'm getting the error?
Is there a better more modern way to implement location updates?
Thanks!
I would recommend you look into the new FusedLocationProviderApi that is part of Google Play Services. From the docs:
The Google Location Services API, part of Google Play Services,
provides a more powerful, high-level framework that automatically
handles location providers, user movement, and location accuracy. It
also handles location update scheduling based on power consumption
parameters you provide. In most cases, you'll get better battery
performance, as well as more appropriate accuracy, by using the
Location Services API.
Make sure you're important the correct LocationClient, should be com.google.android.gms.location.LocationClient and you might be importing android.location.LocationClient
I am developing a location based app which have the functionality to update user current location in every 1 minutes.
I am using bellow to code for requesting location updates:
private LocationRequest mLocationRequest;
private static LocationClient mLocationClient;
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(60000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setFastestInterval(60000);
mLocationClient = new LocationClient(this, this, this);
if (servicesConnected()) {
mLocationClient.connect();
}
servicesConnected() is user defined method which returns true if Google play services is available otherwise returns false
and my overriden method like this:
#Override
public void onConnected(Bundle connectionHint) {
try {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
} catch (IllegalStateException e) {
// TODO: handle exception
}
}
#Override
public void onLocationChanged(Location location) {
// logic to store location data
}
But I found location updates like bellow figure while my GPS is ON :
Please suggest what should I do to overcome unwanted location updates.
Here's some info from OwnTracks https://github.com/owntracks/android/issues/66
From my research the only thing you can do is filter out "bad" locations like this:
#Override
public void onLocationChanged(Location location) {
if(location.getAccuracy() < 210.0) {
// use provided location
}
}
It doesn't appear that there is any way to prevent these updates from being requested using the Fused provider (other than stopping updates or increasing the interval when you have a location you are satisfied with). There's only so much "filtering" that can be done by the GPS itself and those options are the constants inside LocationRequest that you already know about. I believe your issue is hardware related or has to do with the location you are getting updates from (I'm basing this assumption on the OwnTracks data). Google could theoretically offer more advanced Criteria like it used to do with LocationManager, but I believe that would basically do the same thing as my example, except under the hood (i.e. the GPS would still do the work of getting the location and then discard it AFTER it knows the accuracy isn't high enough).
If you want to make it do less work, your best options are increasing the interval or simply stopping updates when you no longer need new locations. For example, if you have a decent location, maybe you raise the interval and keep that location longer. But that depends on what your app is trying to do with the data.
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 :-(