在有来电或去电时检测SIM卡插槽或电话号码

时间:2018-07-14 11:48:25

标签: android broadcastreceiver dual-sim

我一直在开发一个可以检测android中的传入和传出呼叫并记录它们的应用程序。我需要知道哪个模拟用户接听电话或拨打电话。

我已经使广播接收器检测呼入和呼出电话。

这是我的代码:

public class PhoneStateReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    // 1st solution------------------------------

    int whichSim = intent.getIntExtra("simSlot", -1);
    // so this methof return 0            for sim 1 and 1 for sim2
    if (whichSim == -1) {
        whichSim = intent.getIntExtra("com.androie.phone.extra.slot", -1);
    }

// 2nd solution-----------------------------------------------

try {
        Bundle bundl = intent.getExtras();
        int slot = -1;
        if (bundl != null) {
            Set<String> keySet = bundl.keySet();
            for (String key : keySet) {
                switch (key) {
                    case "slot":
                        slot = bundl.getInt("slot", -1);
                        break;
                    case "simId":
                        slot = bundl.getInt("simId", -1);
                        break;
                    case "simSlot":
                        slot = bundl.getInt("simSlot", -1);
                        break;
                    case "slot_id":
                        slot = bundl.getInt("slot_id", -1);
                        break;
                    case "simnum":
                        slot = bundl.getInt("simnum", -1);
                        break;
                    case "slotId":
                        slot = bundl.getInt("slotId", -1);
                        break;
                    case "slotIdx":
                        slot = bundl.getInt("slotIdx", -1);
                        break;
                    default:
                        Log.d("Key:::::", key);

                }
            }
            Log.d("slot", "slot=>" + slot);
        }
    } catch (Exception e) {
        Log.d(TAG, "Exception=>" + e);
    }

    //3rd solution-----------------------------

    int slot = intent.getIntExtra("slot", -1);
    //for outgoing call
    if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {

        phoneNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
        Toast.makeText(context, "OutGoing Call : " + phoneNumber, Toast.LENGTH_LONG).show();
    }
    //incoming call
    else if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {

        String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
        String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        int state = 0;

        //check current and previous state
        if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

            state = TelephonyManager.CALL_STATE_IDLE;
        } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            Intent svc = new Intent(context, RecorderService.class);
            context.startService(svc);
            state = TelephonyManager.CALL_STATE_OFFHOOK;
        } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

            Toast.makeText(context, "Incoming Call Ringing", Toast.LENGTH_SHORT).show();

            state = TelephonyManager.CALL_STATE_RINGING;
        }

        //call state change listener
        onCallStateChanged(context, state, number);

    }
    // incoming sms
    else if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
        Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
        SmsMessage[] msgs = null;

        String msgBody = null;
        if (bundle != null) {
            //---retrieve the SMS message received---
            try {
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                for (int i = 0; i < msgs.length; i++) {
                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    smsNumber = msgs[i].getOriginatingAddress();
                    msgBody = msgs[i].getMessageBody();
                }
            } catch (Exception e) {
                       Log.d("Exception caught",e.getMessage());
            }
        }
        Toast.makeText(context, "SMS Body : " + msgBody, Toast.LENGTH_LONG).show();
        Toast.makeText(context, "SMS IN FROM: " + smsNumber, Toast.LENGTH_LONG).show();

    }
}
public void onCallStateChanged(Context context, int state, String number) {
    if (lastState == state) {
        //No change, debounce extras
        return;
    }
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            isIncoming = true;
            callStartTime = new Date();
            phoneNumber = number;
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //Transition of ringing->offhook are pickups of incoming calls.  Nothing done on them
            if (lastState != TelephonyManager.CALL_STATE_RINGING) {
                isIncoming = false;
                callStartTime = new Date();
                Toast.makeText(context, "Outgoing Call Started", Toast.LENGTH_SHORT).show();
            }


            break;
        case TelephonyManager.CALL_STATE_IDLE:
            //Went to idle-  this is the end of a call.  What type depends on previous state(s)
            Intent intents = new Intent(context, RecorderService.class);
            // stop detect service
            context.stopService(intents);

            if (lastState == TelephonyManager.CALL_STATE_RINGING) {
                //Ring but no pickup-  a miss
                Toast.makeText(context, "Ringing but no pickup" + phoneNumber + " Call time " + callStartTime + " Date " + new Date(), Toast.LENGTH_SHORT).show();
            } else if (isIncoming) {

                Toast.makeText(context, "Incoming " + phoneNumber + " Call time " + callStartTime, Toast.LENGTH_SHORT).show();
            } else {

                Toast.makeText(context, "outgoing " + phoneNumber + " Call time " + callStartTime + " Date " + new Date(), Toast.LENGTH_SHORT).show();

            }

            break;
    }
    lastState = state;
}}

如您所见,我已经尝试了三种不同的解决方案来检测插槽号,但是它们都不起作用。

我需要一个可以检测到SIM卡号或插槽的代码。

谢谢。

0 个答案:

没有答案