简体   繁体   English

识别来自 Sim1 和 Sim2 的通话记录

[英]Identify Call logs from Sim1 and Sim2

I'm trying to show call logs from Sim1 and Sim2 separately.我正在尝试分别显示来自 Sim1 和 Sim2 的通话记录。 I'm getting all call logs, but not able to differentiate it whether it is from Sim1 or Sim2.我得到了所有的通话记录,但无法区分它是来自 Sim1 还是 Sim2。 I have tried below code,我试过下面的代码,

 val managedCursor = activity?.contentResolver?.query(CallLog.Calls.CONTENT_URI, null, null,
        null, null)
    managedCursor?.let {
      val number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER)
      val name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME)
      val type = managedCursor.getColumnIndex(CallLog.Calls.TYPE)
      val date = managedCursor.getColumnIndex(CallLog.Calls.DATE)
      val duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION)
      val simType = managedCursor.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID)
      sb.append("Call Details :")
      while (managedCursor.moveToNext()) {
        val phNumber = managedCursor.getString(number)
        val displayName = managedCursor.getString(name)
        val callType = managedCursor.getString(type)
        val callDate = managedCursor.getString(date)
        val callDayTime = Date(callDate.toLong())
        val callDuration = managedCursor.getString(duration)
        var dir: String = "";
        val dircode = Integer.parseInt(callType)
        val simTypes = managedCursor.getString(simType)
        when (dircode) {
          CallLog.Calls.OUTGOING_TYPE -> dir = "OUTGOING"
          CallLog.Calls.INCOMING_TYPE -> dir = "INCOMING"
          CallLog.Calls.MISSED_TYPE -> dir = "MISSED"
        }
      }
      managedCursor.close()
    }

I have read documents showing PHONE_ACCOUNT_ID may help to identify it.我已阅读显示 PHONE_ACCOUNT_ID 的文档可能有助于识别它。 But it's giving different numbers in different phones.但它在不同的手机中给出不同的数字。

I have also read many SO questions as well, but not able to find any solution.我也阅读了许多 SO 问题,但找不到任何解决方案。

like: 1) How to get SIM1 or SIM2 from call logs programatically 2) how to get call log for sim1, sim2 seperatly from an android activity?比如:1) 如何以编程方式从通话记录中获取 SIM1 或 SIM2 2) 如何从 android 活动中分别获取 sim1、sim2 的通话记录?

Any help will be appreciated, thanks.任何帮助将不胜感激,谢谢。

To start off you mentioned that you are getting different numbers in different phones for the field PHONE_ACCOUNT_ID which is absolutely correct.首先,您提到您在不同的电话中为PHONE_ACCOUNT_ID字段获得不同的号码,这是绝对正确的。

But you don't need to worry about that and it should not bother you.但是您不必担心这一点,它也不应该打扰您。

So Why there are different values for the PHONE_ACCOUNT_ID ?那么为什么PHONE_ACCOUNT_ID有不同的值呢?

Well, subscription_id ie PHONE_ACCOUNT_ID will be unique for each sim card on your device.好吧, subscription_idPHONE_ACCOUNT_ID对于您设备上的每张 sim 卡都是唯一的。 To clear suppose say you have two sim cards on your device then scenario will be like this要清除假设说您的设备上有两张 sim 卡,那么情况将是这样的

  • Sim 1 will have a subscription id as 1 Sim 1 的订阅 ID 为 1
  • Sim 2 will have a subscription id as 2 Sim 2 的订阅 ID 为 2

Now suppose you remove one sim says Sim 1 and enter a new sim which is never inserted on the device before, let's call it as Sim 3. Then this sim will be having subscription_id as 3, not in 1 or 2现在假设您删除了一个 SIM 卡,上面写着 Sim 1,然后输入一个以前从未插入到设备上的新 sim,我们将其称为 Sim 3。那么这个 sim 的订阅 ID 为 3,而不是 1 或 2

Similarly when you insert a 4th sim on the device which is not inserted before it's id will be something other than 1,2,3 But if you insert Sim 1 again it will be having subscription_id as 1 just as before同样,当您在设备上插入第 4 个 SIM 卡时,它的 ID 将不是 1,2,3 但如果您再次插入 Sim 1,它的subscription_id ID 将像以前一样为 1

Note: It's not necessary for subscription_id's to appear in sequential order they can be anything even more than 2 digits注意:subscription_id 不必按顺序出现,它们可以是任何超过 2 位的数字

Now coming to accessing the actual contacts logs related to the sim's present currently on the device is to be done.现在要访问与当前设备上的 sim 相关的实际联系人日志。 But the logs can also have the logs related to other sims ie old sims let's call them old logs.但是日志也可以包含与其他模拟人生相关的日志,即旧模拟人生让我们称它们为旧日志。 So we need to get the current simid for the current sim's you can do it like this所以我们需要为当前的 sim 获取当前的simid你可以这样做

Create A Class Called CallHistory创建一个名为 CallHistory 的Class

data class CallHistory(var number:String,
                       var name:String?,
                       var type:Int,
                       var date:Long,
                       var duration: Long,
                       var subscriberId:String) {

    val cachedName:String  // Separate property for cachedName
        get() {
      return  if(name==null) number else name as String
    }

}

Then Write this function to get the active sim card info然后写这个 function 来获取激活的 sim 卡信息

    /**
     * This Function Will return list of SubscriptionInfo
     */
    private fun getSimCardInfos() : List<SubscriptionInfo>?{
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            val subscriptionManager: SubscriptionManager = getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.READ_PHONE_STATE
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                throw Exception("Permission Not Granted -> Manifest.permission.READ_PHONE_STATE")
            }
            return subscriptionManager.activeSubscriptionInfoList
        }else{
            return null
        }
    }

Write this function to get all call logs编写此 function 以获取所有通话记录

 // This function will return the all Call History
    fun getAllCallHistory() : MutableList<CallHistory>{

        val managedCursor = if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_CALL_LOG
            ) != PackageManager.PERMISSION_GRANTED
        ) {
           throw Exception("Permission Not Granted -> Manifest.permission.READ_CALL_LOG")
        }else{
            this.contentResolver?.query(
                CallLog.Calls.CONTENT_URI, null, null,
                null, null)
        }

        managedCursor?.let {
            val callHistoryList= mutableListOf<CallHistory>()
            while (it.moveToNext())
            {
                callHistoryList.add(CallHistory(number = it.getString(it.getColumnIndex(CallLog.Calls.NUMBER)),
                    name = it.getString(it.getColumnIndex(CallLog.Calls.CACHED_NAME))?:null,
                    type = it.getString(it.getColumnIndex(CallLog.Calls.TYPE)).toInt(),
                    date = it.getString(it.getColumnIndex(CallLog.Calls.DATE)).toLong(),
                    duration = it.getString(it.getColumnIndex(CallLog.Calls.DURATION)).toLong(),
                    subscriberId = it.getString(it.getColumnIndex(CallLog.Calls.PHONE_ACCOUNT_ID)))
                )

             }
            it.close()
            return callHistoryList
        }

        return mutableListOf<CallHistory>()

    }

Now we need to separate call logs based on the subscription_id 's ie PHONE_ACCOUNT_ID but there is a problem with this ie some device store sim ICCID [ It's a 19-20 digit code which is unique for each sim] in PHONE_ACCOUNT_ID but some devices store The actual subscription id which is 1 or 2 digit only.现在我们需要根据subscription_idPHONE_ACCOUNT_ID来分离通话记录,但这有一个问题,即某些设备在PHONE_ACCOUNT_ID中存储 sim ICCID [这是一个 19-20 位的代码,每个 sim 都是唯一的],但有些设备存储实际订阅 ID,仅为 1 位或 2 位数字。 So we need to check whether the subscription_id is equal to original sim subscription_id or it is equal to iccid所以我们需要检查subscription_id是等于原始sim的subscription_id还是等于iccid

Write this function to get Call Logs of only selected sim编写此 function 以获取仅选定 sim 的通话记录

fun getCallHistoryOfSim(simInfo:SubscriptionInfo?, allCallList:MutableList<CallHistory> ) : MutableList<CallHistory> {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1){
       return allCallList.filter { it.subscriberId==simInfo?.subscriptionId.toString() || it.subscriberId.contains(simInfo?.iccId?:"_")}.toMutableList()
    }else{
        throw Exception("This Feature Is Not Available On This Device")
    }

}

So the above function gives logs of the selected sim only所以上面的 function 只给出了所选 sim 的日志

you can call the functions the above functions and get the list.您可以调用上述函数并获取列表。 An example call is like this一个示例调用是这样的

       for(log in getCallHistoryOfSim( getSimCardInfos()?.get(0),getAllCallHistory()) )
        {
            Log.d("Sim1LogDetails", "$log\n____________________________________")
        }

        for(log in getCallHistoryOfSim( getSimCardInfos()?.get(1),getAllCallHistory()) )
        {
            Log.d("Sim2LogDetails", "$log\n____________________________________")

        }

In order to get this code get work you need to specify 2 permissions为了使此代码正常工作,您需要指定 2 个权限

<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

And App Should Target API level 22并且应用程序应针对 API 级别 22

It's Quite a long answer hope you got what It does.这是一个相当长的答案,希望你明白它的作用。 If any doubts let me know如果有任何疑问,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM