android Build.GetSerial()抛出异常 [英] android Build.GetSerial() throwing exception

查看:340
本文介绍了android Build.GetSerial()抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 < uses-permission android:name ="android.permission.READ_PHONE_STATE"/>< uses-permission android:name ="android.permission.READ_PRIVILEGED_PHONE_STATE"/>字符串序列如果(Build.VERSION.SdkInt> = BuildVersionCodes.O){serial = Build.GetSerial();}别的{serial = Build.Serial;} 

我已经在两个不同的设备上(都是android 9.0)尝试过它.可悲的是,当调用GetSerial()函数(华为p10)时,我得到以下异常: Java.Lang.SecurityException:<超时超出了例外详细信息>

在其他设备(银河s8)上,出现此异常:

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

我真的不明白问题出在什么地方,因为我在清单中添加了两个权限,该异常表示...知道我在做什么错吗?

解决方案

您应该遇到权限问题.而且由于Android Marshmallow,您需要向用户询问权限.除了在android清单文件中添加权限外,您还可以添加运行时权限,如下所示:

 静态只读int REQUEST_PHONE_STATE = 1;公共无效checkPermission(){Log.Info(TAG,正在检查权限.");//检查权限是否已经可用.如果(ActivityCompat.CheckSelfPermission(this,Manifest.Permission.ReadPhoneState)!=(int)Permission.Granted){//权限尚未授予RequestPhoneStatePermission();}别的{//权限已经可用,请显示摄像机预览.Log.Info(TAG,权限已被授予.");获取信息();}} 

方法 RequestPhoneStatePermission

 私有无效RequestPhoneStatePermission(){Log.Info(TAG,未授予PhoneState权限.正在请求权限.");如果(ActivityCompat.ShouldShowRequestPermissionRationale(this,Manifest.Permission.ReadPhoneState)){Log.Info(TAG,显示PhoneState权限依据以提供其他上下文.");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);})).展示();}别的{//尚未授予PhoneState权限.直接要求.ActivityCompat.RequestPermissions(this,new String [] {Manifest.Permission.ReadPhoneState},REQUEST_PHONE_STATE);}} 

方法 OnRequestPermissionsResult

 公共重写void OnRequestPermissionsResult(int requestCode,string []权限,Permission [] grantResults){如果(requestCode == REQUEST_PHONE_STATE){//收到摄像机许可的许可结果.Log.Info(TAG,收到的电话状态许可请求的响应.");//检查是否已授予唯一所需的权限if(grantResults.Length == 1&& GrantResults [0] == Permission.Granted){//已授予摄像头权限,可以显示预览Log.Info(TAG,电话状态权限已被授予.正在显示预览.");Snackbar.Make(布局,Resource.String.permission_available_phonestate,Snackbar.LengthShort).Show();获取信息();}别的{Log.Info(TAG,未授予电话状态权限.");Snackbar.Make(布局,Resource.String.permissions_not_granted,Snackbar.LengthShort).Show();}}别的{base.OnRequestPermissionsResult(requestCode,权限,grantResults);}} 

方法 getInfo

  private void getInfo(){字符串序列如果(Build.VERSION.SdkInt> = BuildVersionCodes.O){serial = Build.GetSerial();}别的{serial = Build.Serial;}Log.Info(TAG,"serial =" + serial);} 

有关更多详细信息,您可以检查: https://docs.microsoft.com/zh-cn/xamarin/android/app-fundamentals/permissions?tabs = windows

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

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 file:

<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;
}

I have tried it on two different devices(both android 9.0).Sadly i get the following exception when the GetSerial() function is called(huawei p10): Java.Lang.SecurityException: <Timeout exceeded getting exception details> .

On an other device(galaxy s8) I get this exception:

Java.Lang.SecurityException: getSerial requires READ_PHONE_STATE or READ_PRIVILEGED_PHONE_STATE permission

I really dont understand what the problem is,because I have added both permissions in the manifest,which the exception sais... Any idea what am I doing wrong?

解决方案

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:

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();
        }
    }

Method 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);
        }
    }

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

private void getInfo() {
        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:

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/

这篇关于android Build.GetSerial()抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆