android 抛出异常,android - android Build.GetSerial()抛出异常 - 堆栈内存溢出

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

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

}

这是完整的演示,您可以检查一下。

之后,您可以获得效果: 54cac77e2c7f6ec123df65f838e3c5e4.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值