Marshmallow Maps Permissions

With Marshmallow update being rolled out to almost all latest devices in the market, it is essential for a developer to upgrade their apps to support latest versions. If you are coming from Lollipop or earlier versions of android you are probably stuck when trying to upgrade your app to the new Marshmallow or Nougat which requires special permissions to access the user’s device services. The beauty of Marshmallow and above versions is that it is designed from a user’s perspective whose crucial information stays with the user unless he chooses to share it with an app. And these permissions are app specific, meaning the user has power to choose which app accesses what information on his/her device. It might be bit confusing to deal with all these changes at first for any developer who is very much used to access any service he wanted without user’s consent, but trust me you will get used to it once you start working on it. One of the crucial services we might want to use on most of the apps now a days is user’s location, aka maps. So, to access maps on the user’s device to find their location you need to get past some permissions by asking the user for grants. And this is how you do it.

if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{

//ask for permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {

//have permission already
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, locationListener);
}

Map Permission Screen in MarshmallowThe above code will essentially displays a pop-up asking the user to allow or deny location permission for that particular app. Here the user is in complete control over what services an app has access to, so that the privacy of the user may not be compromised. So, we have to take into account both cases (allowed and denied) and tell the app what to do when permission has been granted. To achieve this, you need to override the onRequestPermissions method as follows. In requestLocationUpdates method you can set the minimum time and minimum distance in milliseconds and meters respectively as per your requirement. If you want to be careful about the user’s battery you need to change these numbers to use the GPS less frequently. And then, link it to the locationListener. Again, for the code to work we need to explicitly check for self permission even though we have checked for grant results above otherwise it is going to throw an error.

 
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{

//whatever you want the app to do after getting permissions locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, locationListener);
}
}
}

So, you have finally achieved user’s location by attaining the permissions needed for your app to work. The above code is written to constantly update the location irrespective of whether there’s a movement or not. Here you can start a new activity and display the location on maps or you can make your own changes after the if statement to make your app act like you want it to. Rest of the code remains same as earlier versions.

2 Comments Add yours

  1. Anil Thappar says:

    Very well explained, made my work easier. Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *