Search code examples
javaandroidandroid-locationlocationmanager

Problems at getting location on Android


I'm new at programming in java and also at programming in Android, and I'm trying to get my location, but I am gettin some troubles.

this is all the code I have written

public class MiServicio extends Service implements LocationListener{private final Context context;
double latitud;
double longitud;
Location location;
boolean gpsActivo;
TextView texto;
LocationManager locationManager;


public MiServicio() {
    super();
    this.context = this.getApplicationContext();
}

public MiServicio(Context c) {
    super();
    this.context = c;
    getLocation();
}

public void setView(View v) {
    texto = (TextView) v;
    texto.setText("Coordenadas: " + latitud + ", " + longitud);
}

public void getLocation() {
    try {
        locationManager = (LocationManager) this.context.getSystemService(LOCATION_SERVICE);
        gpsActivo = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ignored) {
    }

    if (gpsActivo) {


        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this);

        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        latitud = location.getLatitude();
        longitud = location.getLongitude();
    }
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

}

I have found by debugging that it crashes when it reaches the if sentence in this part of the code

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this);

So could someone explain me how to solve this problem?

Regards.

Also here are the permissions I have set in the manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

Edit

These are all the errors I got

and also I use my phone to test this app


Solution

  • When you create a service, you don't call the constructor on it nor do you override it. Instead of using MiServicio servicio = new MiServicio(Context c) in your activity or wherever you are trying to start this service, use the following command:

    context.startService(new Intent(context, MiServicio.class));
    

    Contexts must be implemented by Android and declared in your manifest. For a full tutorial on how to use Android Services, check out the Android Services tutorial.