如何从Android 12的来电中获取电话号码?

uemypmqf  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(191)

我需要从来电中获取phoneber它在Android 12设备下运行良好,使用此方法

  1. val number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)

字符串
但对于 Android 12 及以上设备,(TelephonyManager.EXTRA_INCOMING_NUMBER)数据在Intent中不可用。
清单权限没有问题我可以根据需要添加任何权限。我已在清单中添加此权限

  1. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  2. <uses-permission android:name="android.permission.READ_CALL_LOG" />
  3. <uses-permission android:name="android.permission.WRITE_CALL_LOG" />


我也试过contentResolver查询,但它给出了光标中的倒数第二个数字数据,而不是当前的来电数据。

  1. val cancellationSignal = CancellationSignal()
  2. val cursor = context.contentResolver.query(
  3. CallLog.Calls.CONTENT_URI,
  4. null,
  5. CallLog.Calls.TYPE + " = ?",
  6. arrayOf(CallLog.Calls.INCOMING_TYPE.toString()),
  7. CallLog.Calls.DATE + " DESC",
  8. cancellationSignal
  9. )
  10. if (cursor != null && cursor.moveToFirst()) {
  11. val indexNumber = cursor.getColumnIndex(CallLog.Calls.NUMBER)
  12. val number = cursor.getString(indexNumber)
  13. print(number)
  14. cancellationSignal.cancel()
  15. cursor.close()
  16. }


有谁知道如何找到Android 12及以上设备的号码?

w3nuxt5m

w3nuxt5m1#

在Call中尝试此操作。详情

  1. private fun extractPhoneNumber(callDetails: Call.Details): String? {
  2. val handle = callDetails.handle
  3. if (handle != null) {
  4. // Attempt to extract phone number from the handle
  5. return handle.schemeSpecificPart
  6. } else {
  7. // Handle is null, try other methods if available
  8. val gatewayInfo = callDetails.gatewayInfo
  9. if (gatewayInfo != null) {
  10. return gatewayInfo.originalAddress.schemeSpecificPart
  11. }
  12. }
  13. return null
  14. }

字符串

展开查看全部

相关问题