Skip to content Skip to sidebar Skip to footer

Android Build.getserial() Throwing Exception

I'm developing a Xamarin.Android project,where I need to get the Device Serial number. I have implemented it the way it is shown below. Also added the permissions in the manifest f

Solution 1:

You should faced a persmission issue. And since Android Marshmallow, you need to ask the user for the permissions. Besides adding the permission in android manifest file, you can also add runtime permissions like this:

staticreadonlyint REQUEST_PHONE_STATE = 1;

publicvoidcheckPermission()
    {
        Log.Info(TAG, "Checking permission.");

        // Check if the  permission is already available.if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadPhoneState) != (int)Permission.Granted)
        {

            //  permission has not been granted
            RequestPhoneStatePermission();
        }
        else
        {
            //  permissions is already available, show the camera preview.
            Log.Info(TAG, " permission has already been granted.");
            getInfo();
        }
    }

Method RequestPhoneStatePermission

privatevoidRequestPhoneStatePermission()
    {
        Log.Info(TAG, "PhoneState permission has NOT been granted. Requesting permission.");

        if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.ReadPhoneState))
        {
            Log.Info(TAG, "Displaying PhoneState permission rationale to provide additional context.");

            Snackbar.Make(layout, Resource.String.permission_phonestate_rationale,
                Snackbar.LengthIndefinite).SetAction(Resource.String.ok, new Action<View>(delegate (View obj) {
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadPhoneState }, REQUEST_PHONE_STATE);
                })).Show();
        }
        else
        {
            // PhoneState permission has not been granted yet. Request it directly.
            ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadPhoneState }, REQUEST_PHONE_STATE);
        }
    }

Method OnRequestPermissionsResult

   public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        if (requestCode == REQUEST_PHONE_STATE)
        {
            // Received permission result for camera permission.
            Log.Info(TAG, "Received response for phone state permission request.");

            // Check if the only required permission has been granted
            if (grantResults.Length == 1 && grantResults[0] == Permission.Granted)
            {
                // Camera permission has been granted, preview can be displayed
                Log.Info(TAG, "phonestate permission has now been granted. Showing preview.");
                Snackbar.Make(layout, Resource.String.permission_available_phonestate, Snackbar.LengthShort).Show();

                getInfo();

            }
            else
            {
                Log.Info(TAG, "phonestate permission was NOT granted.");
                Snackbar.Make(layout, Resource.String.permissions_not_granted, Snackbar.LengthShort).Show();
            }
        }
        else
        {
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

method getInfo

privatevoidgetInfo() {
        string serial;
        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            serial = Build.GetSerial();
        }
        else
        {
            serial = Build.Serial;
        }

        Log.Info(TAG, "serial = " + serial);
    }

Here is a full demo, you can check it.

After that, you can get the effect: enter image description here

For more details,you can check: https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows

https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

Post a Comment for "Android Build.getserial() Throwing Exception"