0

I trying to get the location using network/gps.

I define add the permission on the manifest ( ACCESS_FINE_LOCATION ) but i getting that the permission is -1 ( PERMISSION_DENIED )

I calling a instance of the class 'GetLocation' on the main activity. And call the 'getCurrentLocation(this)' from this main activity.

The code:

  public class MainActivity extends AppCompatActivity {

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

    TraceActionLocation t = new TraceActionLocation();

    Location l = t.getCurrentLocation(this);


}


  public class GetLocation
  {
    public Location getCurrentLocation(Context context)
    {
        Location location = null;

    LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

    Boolean isGpsEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    Boolean isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if(!isGpsEnable && !isNetworkEnable)
    {
        // TODO !!! => no gps and no network !!!
    }
    else if(context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_DENIED)
    {
        // 1. get the location from network provider
        if(isNetworkEnable)
        {
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }

        // 2. get the more equate location from the gps
        if(isGpsEnable)
        {
            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }
    }

    return location;
}
}
Yanshof
  • 9,297
  • 18
  • 86
  • 178

2 Answers2

1

Adding the permission for location in manifest for Marshmallow is not enough. You will have to request the permission at runtime. And for that first you will have to add ACCESS_COARSE_LOCATION permission. As I have tried to request ACCESS_FINE_LOCATION at runtime before and that never works. Request for ACCESS_COARSE_LOCATION in your activity. Find a rough example below:

public class MainActivity extends AppCompatActivity {

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


    if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i("result", "permissioin granted");

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

}

bpncool
  • 156
  • 13
-2

Obtaining user location from a mobile device can be complicated. There are several reasons why a location reading (regardless of the source) can contain errors and be inaccurate. Some sources of error in the user location include:

Multitude of location sources GPS, Cell-ID, and Wi-Fi can each provide a clue to users location. Determining which to use and trust is a matter of trade-offs in accuracy, speed, and battery-efficiency. User movement Because the user location changes, you must account for movement by re-estimating user location every so often. Varying accuracy Location estimates coming from each location source are not consistent in their accuracy. A location obtained 10 seconds ago from one source might be more accurate than the newest location from another or same source.

Requesting user permission... ...

Mahachi
  • 11
  • 3