如何在接听电话时获取SIM ID?

时间:2015-04-24 10:56:36

标签: android telephonymanager

这是我的代码,它接听来电并举报号码。我想在双卡手机上运行它,我需要确定哪个SIM卡正在接听来电。

PhoneCallReceiver = new BroadcastReceiver() {

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

        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //Java Reflections
        Class c = null;
        try {

            c = Class.forName(telephonyManager.getClass().getName());
        } catch (ClassNotFoundException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Method m = null;
        try {

            m = c.getDeclaredMethod("getITelephony");
        } catch (SecurityException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        m.setAccessible(true);
        try {

            telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(telephonyManager);
        } catch (IllegalArgumentException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
    }//end onReceive

    PhoneStateListener callBlockListener = new PhoneStateListener() {

        public void onCallStateChanged(int state, String incomingNumber) {

            if (state == TelephonyManager.CALL_STATE_RINGING) {

                if (callBlock.isChecked()==true) {

                    try {
                        Toast.makeText(MainActivity.this, incomingNumber, Toast.LENGTH_SHORT).show();
                    } catch (RemoteException e) {

                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    };
};//end BroadcastReceiver

这只是来袭转号码。我也想吐司机。

1 个答案:

答案 0 :(得分:0)

我不确定添加此代码并尝试

TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String imsi = mTelephonyMgr.getSubscriberId();
    String imei = mTelephonyMgr.getDeviceId(); 
    String simno = mTelephonyMgr.getSimSerialNumber();

和Toast

 Toast.makeText(MainActivity.this, incomingNumber + " to " + imsi +" ", Toast.LENGTH_SHORT).show();