Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out which SIM is making the outgoing call (Dual SIM)

I would like to check which SIM is making an outgoing call for DUAL SIM android phones. Read this article Android : Check whether the phone is dual SIM.

Works like a charm, detects the sim state and if phone is dual SIM. Next step is to get SIM info during an outgoing call request so that i know which SIM is making the call and according to that take some action.

Can someone help me with that?

like image 499
Dionisis K. Avatar asked Sep 06 '13 11:09

Dionisis K.


People also ask

How do you know which number is being called on Dual SIM?

If we understand correctly, you are looking for info on how to identify what number is being called on your iPhone with Dual SIM. When a call is received you should see a letter in a box below the name of the person or number that is calling.

How do I choose which SIM to call from?

To set default SIM for calling in Android smartphones, tap on the settings options and navigate to Network & Internet or Wireless & Networks. Under the option, open the dual SIM settings of your smartphone. In the menu, you will get the option to select a specific SIM when you make a call.

How do I change my call settings on my Dual SIM?

Find and tap Settings > Dual SIM/Dual SIM settings > Dual SIM reachability. Drag the slider beside Dual SIM reachability to the right. The phone numbers of the two inserted SIM cards are entered automatically.

How do I know if Dual SIM is being used?

To see if the phone your using is dual-SIM, go into your phone's Settings app. Tap on Network and internet. The SIM cards option should be right below Airplane mode. If you see that the option shows you two slots for a SIM card, your phone is Dual-SIM.


3 Answers

I've tested and for Jelly Bean I was able to successfully identify the dialling SIM card. Tested also with triple SIM device and worked like a charm.

Code snippet for this inside your BroadcastReceiver:

int whichSIM = 0; // this for security fallback to SIM 1

if (intent.getExtras().containsKey("subscription")) {

    whichSIM = intent.getExtras().getInt("subscription");

}

// do whatever you need to with the information
like image 183
Fernando Rama Avatar answered Oct 20 '22 18:10

Fernando Rama


You can create BroadcastReceiver, which will accept all outgoing calls. Then:

    String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    int slot=intent.getIntExtra("com.android.phone.extra.slot",-1); 
    //0 or 1 are valid in my case

number is dialed number
slot is through which slot you make that call
You will need to register that receiver, and give proper permissions. This works for dialed USSD codes also. It is tested on Privileg GSM S7589 2 SIM cards, Android 4.2.1

like image 33
Stoyan Mihaylov Avatar answered Oct 20 '22 18:10

Stoyan Mihaylov


You can use PhoneStateListener after the call you can determine the outgoing call was from sim 1 or sim2 as shown in the code below.

    private class CallStateListener extends PhoneStateListener {


   @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            //super.onCallStateChanged(state, incomingNumber);

            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                 Log.i(TAG, "Idle " + state);
                //when Idle i.e no call
                if (flag.equals("outgoingcall") ) {

                    // Put in delay because call log is not updated immediately
                    // when state changed
                    // The dialler takes a little bit of time to write to it
                    // 500ms seems to be enough
                    handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // get start of cursor
                            Log.i("CallLogDetailsActivity","Getting Log activity...");

                            cur = ctx.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null,null, CallLog.Calls.DATE + " desc");

                            int number = cur.getColumnIndex(CallLog.Calls.NUMBER);
                            int type = cur.getColumnIndex(CallLog.Calls.TYPE);
                            int date = cur.getColumnIndex(CallLog.Calls.DATE);
                            int duration = cur.getColumnIndex(CallLog.Calls.DURATION);
                            //Check if call was made from sim 1 or sim 2 , if it returns 0 its from sim 1 else if 1 its from sim 2.
                            int idSimId = getSimIdColumn(cur);
                            String callid = "0";

                            if (cur.moveToFirst() == true) {
                                phNumber = cur.getString(number);
                                callType = cur.getString(type);
                                callDate = cur.getString(date);
                                callDayTime = new Date(Long.valueOf(callDate));
                                callDuration = Integer.valueOf(cur.getString(duration));
                                dir = null;
                                int dircode = Integer.parseInt(callType);

                                switch (dircode) {
                                case CallLog.Calls.OUTGOING_TYPE:
                                    dir = "OUTGOING";
                                    break;

                                case CallLog.Calls.INCOMING_TYPE:
                                    dir = "INCOMING";
                                    break;

                                case CallLog.Calls.MISSED_TYPE:
                                    dir = "MISSED";
                                    break;

                                }


                                if(idSimId >= 0){
                                    callid = cur.getString(idSimId);
                                    }


                                cur.close();
                                TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(ctx);

                                boolean isDualSIM = telephonyInfo.isDualSIM();


                                if (isDualSIM) {
                                    if(callid.equals("1")){
                                        simserailno = telephonyInfo.getImeiSIM2();
                                    }else {
                                        simserailno = telephonyInfo.getImeiSIM1();
                                    }
                                } else {

                                    simserailno = tmgr.getSimSerialNumber();
                                }




                                if (tmgr.isNetworkRoaming()) {
                                    roaming = 1;
                                } else {
                                    roaming = 0;
                                }


                                SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


                                StringBuffer sb = new StringBuffer();
                                sb.append("Outgoing Call Log" 
                                        + "\nPhone Number:--- " + phNumber
                                        + " \nCall Type:--- " + dir
                                        + " \nCall Date:--- " + sdfDate.format(Long.valueOf(callDate))
                                        + " \nDual isDualSIM:--- " + isDualSIM
                                        + " \nSIM 1 imei:--- "  + telephonyInfo.getImeiSIM1()
                                        + " \nSIM 2 imei:--- "  + telephonyInfo.getImeiSIM2()
                                        + " \nCalling Sim:--- " + callid
                                        + " \nDevice Number :--- " + Imeinumber
                                        + " \nSim Number :--- " + simserailno
                                        + " \nSubcscriber Number :--- " + subidno
                                        + " \nRoaming :--- " + tmgr.isNetworkRoaming()
                                        + " \nCall duration in sec :--- " + callDuration);
                                sb.append("\n----------------------------------");
                                Log.i("sb", sb.toString());

                                Toast.makeText(ctx, sb.toString(),Toast.LENGTH_LONG).show();

                            }

                            flag = "";


                        }
                    }, 1500);



                }

                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.i(TAG, "offhook " + state);


                    flag= "outgoingcall";


                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.i(TAG, "Ringing " + state);
                  //when Ringing
                 // Log.i(TAG, "Incomng Number to sim1: " + incomingNumber);
                  String msg = "Detected Incoming Call number: " + incomingNumber;
                  Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
                  flag = "oncall";
                break;
            default:
                break;
            }
     }

}

Code snippet for detecting if call was made from sim 1 or sim 2

    public static int getSimIdColumn(final Cursor c) {

    for (String s : new String[] { "sim_id", "simid", "sub_id" }) {
        int id = c.getColumnIndex(s);
        if (id >= 0) {
            Log.d(TAG, "sim_id column found: " + s);
            return id;
        }
    }
    Log.d(TAG, "no sim_id column found");
    return -1;
}
like image 2
Rayyidh Avatar answered Oct 20 '22 17:10

Rayyidh