Locationmanager Is Sending Last Location All Time
Solution 1:
you are using network location which doesn't provide the correct location rather than using network location you should use the GPS location for current and accurate location.
getLastKnowLocation()
method doesn't provide the correct , current location for this you can refer this link.
rather than using network location use GPS location and you can find the good example of GPS location from the given link.
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
Solution 2:
For your concern .. make use of LocationClient instead of LocationManager class...
Try like this ..
LocationClient locationClient; // initialize
Location src; // It will store your location
then in your onCreate method ..
locationClient = newLocationClient(this, this, this);
locationClient.connect(); // this will call OnConnected method of location client@OverridepublicvoidonConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@OverridepublicvoidonConnected(Bundle arg0) {
src = locationClient.getLastLocation();
System.out.println("======================location 1==" + src);
// This is your location update .. it will update each time your location is //changed LocationRequest lrequest = newLocationRequest();
lrequest.setInterval(0);
lrequest.setSmallestDisplacement(0);
locationClient.requestLocationUpdates(lrequest, newLocationListener() {
@OverridepublicvoidonLocationChanged(Location arg0) {
Toast.makeText(getApplicationContext(),
"Location is 12" + arg0.getLatitude(),
Toast.LENGTH_SHORT).show();
}
});
}
@OverridepublicvoidonDisconnected() {
// TODO Auto-generated method stub
}
OR
If you are using Google Map and need to update location of device on Google map then you may make use of Google Map methods as it will provide you current location on device rather than Last location of device ..
Try like this ..
GoogleMap myMap;
Now on Oncreate get your map reference
myMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
myMap.setMyLocationEnabled(true);
Now when you need to get location then try like this ..
Locationlocation=newLocation();
location = myMap.getMyLocation;
That's it .. you are good to go!
Solution 3:
First of all make sure you are including the following two lines of code in the manifest:
<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
Then I would use something similar to the below to get location updates:
publicclassMainActivityextendsFragmentActivityimplementsGooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {
...
// Global variables
...
LocationClient mLocationClient;
boolean mUpdatesRequested;
...
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
...
// Open the shared preferences
mPrefs = getSharedPreferences("SharedPreferences",
Context.MODE_PRIVATE);
// Get a SharedPreferences editor
mEditor = mPrefs.edit();
/*
* Create a new location client, using the enclosing class to
* handle callbacks.
*/
mLocationClient = newLocationClient(this, this, this);
// Start with updates turned off
mUpdatesRequested = false;
...
}
...
@OverrideprotectedvoidonPause() {
// Save the current setting for updates
mEditor.putBoolean("KEY_UPDATES_ON", mUpdatesRequested);
mEditor.commit();
super.onPause();
}
...
@OverrideprotectedvoidonStart() {
...
mLocationClient.connect();
}
...
@OverrideprotectedvoidonResume() {
/*
* Get any previous setting for location updates
* Gets "false" if an error occurs
*/if (mPrefs.contains("KEY_UPDATES_ON")) {
mUpdatesRequested =
mPrefs.getBoolean("KEY_UPDATES_ON", false);
// Otherwise, turn off location updates
} else {
mEditor.putBoolean("KEY_UPDATES_ON", false);
mEditor.commit();
}
}
...
/*
* Called by Location Services when the request to connect the
* client finishes successfully. At this point, you can
* request the current location or start periodic updates
*/@OverridepublicvoidonConnected(Bundle dataBundle) {
// Display the connection statusToast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
// If already requested, start periodic updatesif (mUpdatesRequested) {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
}
}
I would recommend you take a look at this tutorial. Hope it helps you :)
Solution 4:
Although the best option is to switch to new Location API based on Google Play Services (http://developer.android.com/training/location/index.html), here I am trying to explain what is wrong.
You are calling the setListener() method only once in onResume(). So the listener starts listening. But on any location change you are clearing the listener. As a result you'll get a last known location at first and then you'll get maximum one location update. After that as the listener is set as null, you'll not get any update. So you can fix it simply by modifying the TimeoutTask.
publicclassTimeoutTaskimplementsRunnable {
private CustomLocationListener listener;
publicTimeoutTask(CustomLocationListener listener) {
this.listener = listener;
}
@Override
publicvoidrun() {
// TODO Auto-generated method stubif (listener == null || listener.isCompleted()) {
Log.e("provider", "completed");
System.out.println("Lat in completed= " + loc.getLatitude());
System.out.println("Long in completed = " + loc.getLongitude());
} else {
Log.e("provider", "timeout");
clearListener();
setUserPoint();
}
// setting the listener again
setListener();
}
}
Post a Comment for "Locationmanager Is Sending Last Location All Time"