Flutter中如何判断通话记录来自SIM卡1还是SIM卡2
判断通话记录归属SIM卡1或SIM卡2的方法(call_log: ^3.2.1)
在call_log库的CallLogEntry对象中,有两个核心字段可以用来区分通话记录对应的SIM卡:
1. 直接使用simDisplayName字段
这个字段返回SIM卡的显示名称,多数设备会默认显示为SIM 1、SIM 2,或者用户自定义的运营商名称(如“中国移动”、“中国电信”)。你可以直接通过判断该字段的值确定归属:
修改你的代码,添加SIM归属判断:
for (CallLogEntry entry in _callLogEntries) { String simOwner; if (entry.simDisplayName == "SIM 1") { simOwner = "SIM卡1"; } else if (entry.simDisplayName == "SIM 2") { simOwner = "SIM卡2"; } else { simOwner = entry.simDisplayName ?? "未知SIM"; } children.add( Column( children: <Widget>[ const Divider(), Text('F. NUMBER : ${entry.formattedNumber}', style: mono), Text('C.M. NUMBER: ${entry.cachedMatchedNumber}', style: mono), Text('NUMBER : ${entry.number}', style: mono), Text('NAME : ${entry.name}', style: mono), Text('TYPE : ${entry.callType}', style: mono), Text('DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.timestamp)}', style: mono), Text('DURATION : ${entry.duration}', style: mono), Text('ACCOUNT ID : ${entry.phoneAccountId}', style: mono), Text('SIM NAME : ${entry.simDisplayName}', style: mono), Text('SIM 归属 : $simOwner', style: mono), // 新增SIM归属判断行 ], crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start, ), ); }
2. 使用phoneAccountId字段
这个字段是系统为每张SIM卡分配的唯一标识,通常是"1"、"2"这类简单字符串(不同设备可能有差异)。你可以通过该字段的值对应SIM卡序号:
示例代码:
for (CallLogEntry entry in _callLogEntries) { String simOwner; if (entry.phoneAccountId == "1") { simOwner = "SIM卡1"; } else if (entry.phoneAccountId == "2") { simOwner = "SIM卡2"; } else { simOwner = "未知SIM"; } // 后续Widget构建代码同上... }
注意事项
不同品牌、型号的设备,simDisplayName和phoneAccountId的返回格式可能不同,建议先打印这两个字段的实际值,再根据设备的实际返回结果调整判断逻辑。
内容的提问来源于stack exchange,提问作者Anandh Krishnan
相关产品推荐
相关产品推荐

