0

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

  • What errors are you getting ? Show the logcat – tahsinRupam May 03 '17 at 04:23
  • what is your emulator? or Is device location turn on? – Yashar Panahi May 03 '17 at 04:25
  • Refer [This Link](http://stackoverflow.com/questions/37322645/nullpointerexception-when-trying-to-check-permissions) . I hope this will help to solve your issue. – priyanka kamthe May 03 '17 at 05:21
  • get location using LocationManager is old fashioned, you can use GoogleApiClient. Here is sample code: http://stackoverflow.com/a/33419346/3073945 To get location permission from Android 6.0 you have to get runtime permission. Here has the code for getting runtime permission http://stackoverflow.com/a/38142728/3073945 – Md. Sajedul Karim May 03 '17 at 06:37

3 Answers3

0

Try to use following code in if() statement :

  if (context.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;
    }
R.R.M
  • 780
  • 4
  • 10
0

Kindly replace your code for checking and granting permission by below code

     int permissionCheck = ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            checkPermission();
        }



private void checkPermission() {
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {


        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            // Show an explanation to the user asynchronously -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
            return;
        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
            // MY_PERMISSIONS_REQUEST_READ_PHONE_STATE is an
            // app-defined int constant. The callback method gets the
            // result of the request.
            return;
        }
    }
}
Akash Dubey
  • 1,508
  • 17
  • 34
0

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.

Graham
  • 7,431
  • 18
  • 59
  • 84
Pablo Baxter
  • 2,144
  • 1
  • 17
  • 36