1. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    2. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    3. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    1. //动态权限判断
    2. val tm: TelephonyManager =
    3. getSystemService(Context.TELEPHONY_SERVICE)as TelephonyManager
    4. val sm = getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)as SubscriptionManager
    5. val subId =if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    6. SubscriptionManager.getDefaultDataSubscriptionId()
    7. }else{
    8. SmsManager.getDefaultSmsSubscriptionId()
    9. }
    10. val index = sm.activeSubscriptionInfoList.firstOrNull { it.subscriptionId == subId }?.simSlotIndex?:0
    11. val cellList = tm.allCellInfo
    12. val info = cellList.filter { it.isRegistered }.getOrNull(index)
    13. var cellId = -1L
    14. if (info is CellInfoGsm) {
    15. info.cellIdentity.cid.let {
    16. it > 0
    17. cellId = it.toLong()
    18. }
    19. } else if (info is CellInfoCdma) {
    20. info.cellIdentity.basestationId.let {
    21. it > 0
    22. cellId = it.toLong()
    23. }
    24. } else if (info is CellInfoLte) {
    25. info.cellIdentity.ci.let {
    26. it > 0
    27. cellId = it.toLong()
    28. }
    29. } else if (info is CellInfoWcdma) {
    30. info.cellIdentity.cid.let {
    31. it > 0
    32. cellId = it.toLong()
    33. }
    34. } else if (info is CellInfoNr) {
    35. (info.cellIdentity as CellIdentityNr).nci.let {
    36. it > 0
    37. cellId = it
    38. }
    39. }