java 在Android CallLogCalls中使用PHONE_ACCOUNT_ID识别SIM卡插槽

vc9ivgsu  于 2022-12-28  发布在  Java
关注(0)|答案(2)|浏览(314)

在双SIM卡手机中,我使用PHONE_ACCOUNT_ID属性在通话记录中区分SIM卡,如下面的代码所示。(1或2)拨打或接听电话。电话帐户ID显示类似于8953011201104578086F的内容,用于一张SIM卡和类似卡,但无法与另一个相提并论。这是在三星移动的上测试的:

fun readCallLog() {
    val cursor = context.contentResolver.query(CallLog.Calls.CONTENT_URI,null, null, null, CallLog.Calls.DATE + " DESC")
    val number = cursor?.getColumnIndex(CallLog.Calls.NUMBER)
    val date = cursor?.getColumnIndex(CallLog.Calls.DATE)
    val type = cursor?.getColumnIndex(CallLog.Calls.TYPE)
    val account_id = cursor?.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID)

    val tmp : MutableList<List<String?>> = mutableListOf()

    while (cursor?.moveToNext() == true ) {
        val call_number = if (number != null) cursor.getString(number) else ""
        val call_date = if(date != null) cursor.getString(date) else ""
        val call_type = if(type != null) cursor.getInt(type).toString() else ""
        val call_account_id = if(account_id != null) cursor.getString(account_id) else ""

        tmp.add( listOf(call_number, call_date, call_type, call_account_id))
    }
}
dldeef67

dldeef671#

您可以使用SubscriptionManager.getActiveSubscriptionInfoList()获取SIM卡的信息。
在某些设备上,Call.PHONE_ACCOUNT_ID等于subscriptionInfo.getSubscriptionId(),但是在其他设备(您的情况)上,subscriptionInfo.getIccId()是它的子字符串,因此您需要检查两者。
另请参见SubscriptionManager参考。

vwoqyblh

vwoqyblh2#

正式的方法是检查帐户ID(文档此处),但在某些设备上,它只返回SIM卡插槽索引,因此下面是解决方法的代码(来自Android 6.0(Marshmallow)):

fun getSimSlotIndexFromAccountId(context: Context, accountIdToFind: String): Int {
    // This is actually the official data that should be found, as on the emulator, but sadly not all phones return here a proper value
    val telecomManager = context.getSystemService<TelecomManager>()
    telecomManager.callCapablePhoneAccounts.forEachIndexed { index, account: PhoneAccountHandle ->
        val phoneAccount: PhoneAccount = telecomManager.getPhoneAccount(account)
        val accountId: String = phoneAccount.accountHandle
            .id
        if (accountIdToFind == accountId) {
            return index
        }
    }
    accountIdToFind.toIntOrNull()?.let {
        if (it >= 0)
            return it
    }
    return -1
}

用法:

val simIdColumnIndex = callLogCursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID)
val accountId: String = callLogCursor.getString(simIdColumnIndex)
val simCardSlotIndex = getSimSlotIndexFromAccountId(applicationContext, accountId)

我在这里报告了这个问题(有些设备不遵循官方API):

  • 一个月一次 *

相关问题