如何检测新的拨出电话双卡设备中的SIM卡?

时间:2018-02-19 14:12:18

标签: java android telecommunication

我知道我可以通过此接收器检测到新的拨出电话:

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

在OnReceive方法中,我想知道哪个sim进行此调用?

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 
  }
}

2 个答案:

答案 0 :(得分:2)

广播接收器收到的意图应该在捆绑中有一些额外的信息,其中一个是“插槽” - 意思是SIM插槽。

你可以在上面的例子中得到这个 - 这是针对API 22及更高版本的:

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
       }
    }
}

答案 1 :(得分:0)

我认为这会更正确

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)