Demonic217 Demonic217 - 3 months ago 13
Android Question

Not getting prompt to allow location permissions (Android)

I'm designing an application that uses the users location and after a day of nothing working I found the solution, I had to go into my phones settings,find my app and manually give the application permission to use my location.

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// mMap.addMarker(new MarkerOptions().position(MyCords()).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(MyCords()));
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
mMap.setMyLocationEnabled(true);
}

public LatLng MyCords() {
LocationManager LM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return null;
}
Location MyLocat = LM.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng myCords = new LatLng(MyLocat.getLatitude(),MyLocat.getLongitude());
return myCords;
}
}


This is my MapActivity

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


These are the permissions in my manifest file.

My question is how do I prompt the user for permission instead of having to go turn it on manually ?

Thanks

Answer

to check location permission

int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 101;
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);
        }

and to handle request

@Override
    public void onRequestPermissionsResult(int requestCode,  String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission granted.
                } else {
                    // permission denied.
                }
            }
        }
    }