[android] How does it work - requestLocationUpdates() + LocationRequest/Listener

I am new Android coder and I have problem with requesting updates for my localization.

I working with tutorials from http://developer.android.com/training/location/receive-location-updates.html .

My application can handle exceptions, getting latitude and longitute properly, and geocoder can handle displaying the adress. But I ask for location only once - or when location changes. I would like to do time intervals. For now I started implementing code from the tutorials and it looks like that:

public class MainActivity extends Activity implements 
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {

private static final int MILLISECONDS_PER_SECOND = 1000;

public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
private static final long UPDATE_INTERVAL =
          MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;

private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
private static final long FASTEST_INTERVAL =
          MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;

private TextView tvStatus;
private TextView tvLatitude;
private TextView tvLongitude;

LocationRequest mLocationRequest;
LocationClient mLocationClient;
Location mCurrentLocation;

boolean bNetworkEnabled;
boolean bGPSEnabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    tvStatus = (TextView)findViewById(R.id.tvStatus);
    tvLatitude = (TextView)findViewById(R.id.tvLatitude);
    tvLongitude = (TextView)findViewById(R.id.tvLongitude);

    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationClient = new LocationClient(this, this, this);

    checkProviders();
}

So there are already intervals implemented and location request. But in the link I gave before there is a comment that I should use somewhere requestLocationUpdates() (probably onCreate(), onStart() and removal of request on onStop()), but I have problem with it. So, Eclipse shows me 3 methods:

requestLocationUpdates(LocationRequest request, LocationListener listener)
requestLocationUpdates(LocationRequest request, PendingIntent CallbackIntent)
requestLocationUpdates(LocationRequest request, LocationListener listener, Looper looper)

So the first one I think is most right in this place. What should I place in LocationListener slot? I ask for help with little explanation how it works.

This question is related to android location updates locationlistener

The answer is


You are implementing LocationListener in your activity MainActivity. The call for concurrent location updates will therefor be like this:

mLocationClient.requestLocationUpdates(mLocationRequest, this);

Be sure that the LocationListener you're implementing is from the google api, that is import this:

import com.google.android.gms.location.LocationListener;

and not this:

import android.location.LocationListener;

and it should work just fine.

It's also important that the LocationClient really is connected before you do this. I suggest you don't call it in the onCreate or onStart methods, but in onResume. It is all explained quite well in the tutorial for Google Location Api: https://developer.android.com/training/location/index.html


I use this one:

LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

For example, using a 1s interval:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);

the time is in milliseconds, the distance is in meters.

This automatically calls:

public void onLocationChanged(Location location) {
    //Code here, location.getAccuracy(), location.getLongitude() etc...
}

I also had these included in the script but didnt actually use them:

public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}

In short:

public class GPSClass implements LocationListener {

    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
    }
}

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to location

Nginx serves .php files as downloads, instead of executing them Get User's Current Location / Coordinates Location Services not working in iOS 8 How to set fake GPS location on IOS real device Android Google Maps API V2 Zoom to Current Location What is meaning of negative dbm in signal strength? How does it work - requestLocationUpdates() + LocationRequest/Listener How to get Android GPS location Redirect using AngularJS How to check if Location Services are enabled?

Examples related to updates

Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes Update a dataframe in pandas while iterating row by row How does it work - requestLocationUpdates() + LocationRequest/Listener Run local java applet in browser (chrome/firefox) "Your security settings have blocked a local application from running" How do I update a GitHub forked repository? How to know when a web page was last updated?

Examples related to locationlistener

How does it work - requestLocationUpdates() + LocationRequest/Listener