Yanshof Yanshof - 4 months ago 12
Android Question

fail to get gps/network location even when i set the location permission

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;
}
}

Answer

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
    }
}

}

Comments