<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
//动态权限判断
val tm: TelephonyManager =
getSystemService(Context.TELEPHONY_SERVICE)as TelephonyManager
val sm = getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)as SubscriptionManager
val subId =if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
SubscriptionManager.getDefaultDataSubscriptionId()
}else{
SmsManager.getDefaultSmsSubscriptionId()
}
val index = sm.activeSubscriptionInfoList.firstOrNull { it.subscriptionId == subId }?.simSlotIndex?:0
val cellList = tm.allCellInfo
val info = cellList.filter { it.isRegistered }.getOrNull(index)
var cellId = -1L
if (info is CellInfoGsm) {
info.cellIdentity.cid.let {
it > 0
cellId = it.toLong()
}
} else if (info is CellInfoCdma) {
info.cellIdentity.basestationId.let {
it > 0
cellId = it.toLong()
}
} else if (info is CellInfoLte) {
info.cellIdentity.ci.let {
it > 0
cellId = it.toLong()
}
} else if (info is CellInfoWcdma) {
info.cellIdentity.cid.let {
it > 0
cellId = it.toLong()
}
} else if (info is CellInfoNr) {
(info.cellIdentity as CellIdentityNr).nci.let {
it > 0
cellId = it
}
}