如何查找正在进行的呼出电话是哪张SIM卡(双卡)

7
我想要检查哪张SIM卡发出了双卡安卓手机的外拨电话。阅读此文章Android: Check whether the phone is dual SIM,它可以轻松地检测SIM状态以及手机是否为双卡。接下来需要获取在外拨通话请求期间的SIM信息,以便我知道哪张SIM卡正在发起通话,并根据此采取一些措施。请问有人能帮忙吗?
5个回答

7

我已经进行了测试,对于Jelly Bean版本,我成功地识别了拨号SIM卡。同时,我还测试了三重SIM设备,结果非常好。

以下是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

2

您可以创建一个BroadcastReceiver,来接收所有的拨出电话。然后:

    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是拨打的号码
slot是您进行通话的插槽
您需要注册该接收器,并授予适当的权限。 此方法也适用于拨打的USSD代码。 在Privileg GSM S7589 2 SIM卡,Android 4.2.1上已经测试。


测试了两部手机(1个特权)(始终获得插槽-1) 内联`public class OutgoingCallInterceptor extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); int slot=intent.getIntExtra("com.android.phone.extra.slot",-1); //0或1在我的情况下是有效的 Toast.makeText(context.getApplicationContext(), "插槽:" + slot + ",号码:" + number, Toast.LENGTH_LONG).show(); // 终止旧呼叫 abortBroadcast(); setResultData(null); }}` - Dionisis K.

2
您可以在通话后使用PhoneStateListener来确定下行呼叫是从SIM卡1还是SIM卡2发出的,如下所示的代码。
    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;
            }
     }

}

检测呼叫是从SIM卡1还是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;
}

callid = cur.getString(idSimId) 返回类似于“899103011100......”的号码,我认为这是一个“ICCID”。 - Naimish B. Makwana
你能否发布完整的类?cur、tmgr、dir、flag是什么?请更新。 - Android

0

-1

在数据库CallLog.Calls中有一个network,您可以从那里获取它。


怎么做?你能解释一下吗? - Rachcha
这个能用来获取呼出请求时的信息吗?如果可以,您能解释一下吗? - Flygenring
1
CallLog.Calls数据库中没有网络这个字段,一些厂商使用sim_id或simno,但每个厂商都有不同的实现方式,目前还没有通用的实现。 - dirtydexter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接