网上可以查到这个例子,不过要改下:
最后加上
if(!serial || serial=="unknown"){
if (SystemProperties.get('ro.serialno')) {
serial = SystemProperties.get('ro.serialno')
}
}
完整代码:
getserial() {
var Build = plus.android.importClass("android.os.Build");
var Manifest = plus.android.importClass("android.Manifest");
var MainActivity = plus.android.runtimeMainActivity();
var SystemProperties = plus.android.importClass("android.os.SystemProperties")
var serial = ''
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { //9.0+
var ArrPermissions = [
Manifest.permission.READ_PHONE_STATE,
];
function PermissionCheck(permission) {
if (Build.VERSION.SDK_INT >= 23) {
if (MainActivity.checkSelfPermission(permission) == -1) {
return false;
}
}
return true;
}
function PermissionChecks(Arr) {
var HasPermission = true;
for (var index in Arr) {
var permission = Arr[index];
//如果此处没有权限,则是用户拒绝了
if (!PermissionCheck(permission)) {
HasPermission = false;
break;
}
}
return HasPermission;
}
function PermissionRequest(Arr) {
var REQUEST_CODE_CONTACT = 101;
if (Build.VERSION.SDK_INT >= 23) {
MainActivity.requestPermissions(Arr, REQUEST_CODE_CONTACT);
}
}
//如果没有权限,则申请权限
if (!PermissionChecks(ArrPermissions)) {
PermissionRequest(ArrPermissions); //此处申请权限,会弹出一个让你授权可获取设备号信息权限框
} else { //如果拥有权限
serial = Build.getSerial()
}
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { //8.0+
if (Build.SERIAL) {
serial = Build.SERIAL;
}
} else {
if (SystemProperties.get('ro.serialno')) {
serial = SystemProperties.get('ro.serialno')
}
}
if(!serial || serial=="unknown"){
if (SystemProperties.get('ro.serialno')) {
serial = SystemProperties.get('ro.serialno')
}
}
if (serial) {
serial = serial.trim()
}
uni.setStorageSync('serial', serial);
return serial
}