0

error: no suitable method found for requestLocationUpdates(String,int,int,com.google.android.gms.location.LocationListener)

import android.content.Context;
import android.content.Intent;
import android.location.Location;
//import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.android.modulousina.R;
import com.google.android.gms.location.LocationListener;


public class MainActivity extends AppCompatActivity{

Context context = this;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 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.

            String myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude() + "Precisao: " + Float.toString(location.getAccuracy());

            Log.e("MY CURRENT LOCATION", myLocation);

        }

        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.GPS_PROVIDER, 0, 0, locationListener);
    }
}

On AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

I'm getting an error on requestLocationUpdates method. Can you help me ?

Lucas Alc
  • 1
  • 3
  • You're using the wrong LocationListener, import android.location.LocationListener instead. – Daniel Nugent Apr 29 '16 at 14:45
  • I was using import com.google.android.gms.location.LocationListener. I traded to import android.location.LocationListener and now I get and error on the same line that says: "Call requires permission which may be rejected by user: code should explicity check to see if permission is available (with 'CheckPermission') or explicity handle a potential 'SecurityException') – Lucas Alc Apr 29 '16 at 15:06
  • If you target api-23 then you need to handle requesting permissions at runtime, see here for a Location permission example: http://stackoverflow.com/questions/34582370/how-can-i-use-google-maps-and-locationmanager-to-show-current-location-on-androi – Daniel Nugent Apr 29 '16 at 16:03

0 Answers0