How can i detect the New outgoing call From which sim in dual sim devices?

情到浓时终转凉″ 提交于 2019-12-23 05:15:55

问题


I know that i can detect new outgoing call by this receiver :

<receiver android:name=".NewOutgoingCallReceiver">
   <intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
   </intent-filter>
  </receiver> 

And in OnReceive method i want to know which sim making this call ?

public class NewOutgoingCallReceiver extends BroadcastReceiver
{

@Override
public void onReceive( Context context, Intent intent )
  {
     // here i want to check which sim is making that new call 
  }
}

回答1:


The intent received by your broadcast receiver should have some extra information in the bundle, one of which is the 'slot' - meaning the SIM slot.

You can get this in your example above like this - this is for API 22 and above:

public class NewOutgoingCallReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive( Context context, Intent intent )
    {
       //check which sim is making that new call 
       String callSlot = "";
       Bundle bundle = intent.getExtras();
       callSlot =String.valueOf(bundle.getInt("slot", -1));
       if(callSlot == "0"){
           //Call is from SIM slot0
       } else if(callSlot =="1"){
          //Call is from SIM slot 1
       }
    }
}



回答2:


I think this will be more correct

val SIM_SLOT_NAMES = arrayOf(
    "extra_asus_dial_use_dualsim",
    "com.android.phone.extra.slot",
    "slot",
    "simslot",
    "sim_slot",
    "subscription",
    "Subscription",
    "phone",
    "com.android.phone.DialingMode",
    "simSlot",
    "slot_id",
    "simId",
    "simnum",
    "phone_type",
    "slotId",
    "slotIdx"
)

private fun getSimSlot(extras: Bundle?): Int {
    for (name in SIM_SLOT_NAMES) {
        if (extras?.containsKey(name) == true) {
            return extras.getInt(name, 0)
        }
    }
    return 0
}

// usage
getSimSlot(intent.extras)


来源:https://stackoverflow.com/questions/48868040/how-can-i-detect-the-new-outgoing-call-from-which-sim-in-dual-sim-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!