Xamarin.Forms Runtime Permission Handling above Android 6.0

After Android 6.0, we have to using Runtime Persmissions. No more enaugth to create an Android.Manifest file. How can we correctly use it?

Step 1: Android.Manifest file

Still we have to create an Android.Manifest file, but a little bit otherwise. We have to use this way: https://developer.android.com/training/permissions/requesting#java. So, if we have used it so far as “uses-permission” before, from now we have to use it as “uses-permisson-sdk-<SDK_Version_Number>”. We can specify the max SDK version too.

<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.ACCESS_FINE_LOCATION" />

We have to specify the minimum SDK version of the application (https://developer.android.com/guide/topics/manifest/uses-sdk-element), because, if we don’t do, the minimum SDK version will be the 1.0. We can define the target and the max SDK version as well.

<uses-sdk android:minSdkVersion="23" />

Example Android.Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.companyname" android:versionCode="1" android:versionName="1.0.15" android:installLocation="auto">
	<uses-sdk android:minSdkVersion="23" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.ACCESS_FINE_LOCATION" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.ACCESS_COARSE_LOCATION" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.ACCESS_MOCK_LOCATION" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.ACCESS_WIFI_STATE" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.INTERNET" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.READ_EXTERNAL_STORAGE" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.RECEIVE_SMS" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.SEND_SMS" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.WRITE_SMS" />
	<uses-permission-sdk-23 android:maxSdkVersion="28" android:name="android.permission.READ_SMS" />
	<application android:label="Application" android:icon="@drawable/icon">
		
	</application>
</manifest>

Step 2: Runtime Permission

Open the MainActivity.cs file in the Android Project. We have to create a Request to the user: “hello we have to use the calendar, location, sms, etc. when this app running”. The user will deside that, he/she will use these or not.

                var requiredPermissions = new string[]
                {
                    Manifest.Permission.AccessFineLocation,
                    Manifest.Permission.AccessCoarseLocation,
                    Manifest.Permission.AccessNetworkState,
                    Manifest.Permission.AccessLocationExtraCommands,
                    Manifest.Permission.AccessMockLocation,
                    Manifest.Permission.AccessNetworkState,
                    Manifest.Permission.AccessWifiState,
                    Manifest.Permission.Internet,
                    Manifest.Permission.WriteExternalStorage,
                    Manifest.Permission.ReadExternalStorage,
                    Manifest.Permission.ReceiveSms,
                    Manifest.Permission.SendSms,
                    Manifest.Permission.WriteSms,
                    Manifest.Permission.ReadSms
                };

                RequestPermissions(requiredPermissions, 1);

What have we done? We said that these permissions are neccessary to use at runtime and we created the request to the user. If any of theese permissions is not granted, it will be asked at the start of the application. If all were granted, nothing will happen. Okay, but what is “1” (the second param of the Request)? This is the request code, this can be another too, this is an app-defined code.

If we are corious, that our request is granted or not, we can override the OnRequestPermissionsResult() function.

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

This function will response our permission request(s) result(granted or not).

Leave a Reply

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