0

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 
  }
}
Mustafa
  • 21
  • 4

2 Answers2

2

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
       }
    }
}
Mick
  • 24,231
  • 1
  • 54
  • 120
  • 1
    Mick , thanks for your answer but it not working , it always return -1 . intent only have one extra which is Intent.EXTRA_PHONE_NUMBER . – Mustafa Mar 04 '18 at 07:55
  • Firstly, I should have said this is for API 22 and above - updated now. There is some reports the behaviour may be device specific. If you device is above API 22 then it may be worth experimenting. I don't know of any other 'official' approach although some have reported success using reflection to expose unofficial APIs: https://stackoverflow.com/a/32871156/334402 – Mick Mar 04 '18 at 11:05
0

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)
Vlad
  • 7,997
  • 3
  • 56
  • 43