【问题标题】:android Build.GetSerial() throwing exceptionandroid Build.GetSerial() 抛出异常
【发布时间】:2019-06-20 12:15:51
【问题描述】:

我正在开发一个 Xamarin.Android 项目,我需要在其中获取设备序列号。

我已经按照如下所示的方式实现了它。 还在清单文件中添加了权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />

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

我已经在两个不同的设备上尝试过(都是android 9.0)。遗憾的是,当调用GetSerial()函数时出现以下异常(华为p10):Java.Lang.SecurityException: &lt;Timeout exceeded getting exception details&gt; .

在其他设备上(galaxy s8)我得到这个异常:

Java.Lang.SecurityException: getSerial 需要 READ_PHONE_STATE 或 READ_PRIVILEGED_PHONE_STATE 权限

我真的不明白问题是什么,因为我已经在清单中添加了这两个权限,异常是...... 知道我做错了什么吗?

【问题讨论】:

标签: android xamarin.android android-permissions


【解决方案1】:

您应该面临权限问题。并且由于 Android Marshmallow,您需要向用户询问权限。 除了在 android manifest 文件中添加权限外,您还可以像这样添加运行时权限:

static readonly int REQUEST_PHONE_STATE = 1;

public void checkPermission()
    {
        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();
        }
    }

方法RequestPhoneStatePermission

 private void RequestPhoneStatePermission()
    {
        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);
        }
    }

方法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);
        }
    }

方法getInfo

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

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

Here 是一个完整的演示,你可以查看它。

之后就可以得到效果了:

更多详情,您可以查看: https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows

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

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 2012-09-30
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2016-02-08
    • 2013-02-14
    • 1970-01-01
    相关资源
    最近更新 更多