Reputation: 11
I started programming recently and im trying to make a basic map aplication with the location button enabled, in android 6.0 Marshmallow. I think i have understood how works the new permission model. When i run the app, it asks me to give location permission but, when i give it, the location button doesn´t appears. If i restart the app it already appears. I will put the code of my onMapReady method:
@Override
public void onMapReady(GoogleMap map) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
map.setMyLocationEnabled(true);
} else {
// Show rationale and request permission.
Toast toast = Toast.makeText(this, "need permission", Toast.LENGTH_LONG);
toast.show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
}
}
map.setMyLocationEnabled(true);
}
I've tried to change the last setMyLocationEnabled inside the else but it didn't work. I know that it's a little silly question but i don't know how to solve it. Hope someone can help me. Thanks in advance
Upvotes: 1
Views: 963
Reputation: 494
You may add the code below.
In earlier version of Android, at installation time, it shows users the permission dialog and they were granted the defined permission in your manifest file to the application.
This changed in Marshmallow. Now, each application should have to ask permission from user to access the it.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
System.out.println("CHECK_RUN_TIME_PERMISSION_IF_MARSHMELLOW");
if(!checkPermission()) {
requestPermission();
}else {
System.out.println("CHECK_RUN_TIME_PERMISSION_IF_MARSHMELLOW++");
}
}
private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
private void requestPermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)){
Toast.makeText(MainActivity.this,"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,"Permission Granted, Now you can access location data.",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"Permission Denied, You cannot access location data.",Toast.LENGTH_LONG).show();
}
break;
}
}
Upvotes: 1