Android如何从双卡手机中获取电话号码

时间:2014-03-27 06:17:03

标签: android telephonymanager

你好朋友在我的应用程序中我想向用户显示他们的SIM卡电话号码,但在双卡手机中没有给任何类型号码我谷歌搜索但不准确。请帮助我从双卡通电话号码。 请注意以下内容不适用于双SIM卡以获取数字:

       String phonenumber = telephonymanager.getLine1Number();

2 个答案:

答案 0 :(得分:2)

telephonymanager.getLine1Number()不保证返回Sim号码,因为电话号码并未实际存储在所有Sim卡上。

更好的选择是向用户询问一次电话号码,并通过向该号码发送消息来确认。

Here对此有更好的解释。

答案 1 :(得分:1)

使用以下代码。

如下所示制作类SimNoInfo。

import java.lang.reflect.Method;

import android.content.Context;
import android.telephony.TelephonyManager;

public final class SimNoInfo {

private static SimNoInfo telephonyInfo;
private String imeiSIM1;
private String imeiSIM2;
private boolean isSIM1Ready;
private boolean isSIM2Ready;

public String getImeiSIM1() {
    return imeiSIM1;
}

public String getImeiSIM2() {
    return imeiSIM2;
}


public boolean isSIM1Ready() {
    return isSIM1Ready;
}

public boolean isSIM2Ready() {
    return isSIM2Ready;
}


public boolean isDualSIM() {
    return imeiSIM2 != null;
}

private TelephonyInfo() {
}

public static SimNoInfo getInstance(Context context){

    if(telephonyInfo == null) {

        telephonyInfo = new SimNoInfo();

        TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));

        telephonyInfo.imeiSIM1 = telephonyManager.getDeviceId();;
        telephonyInfo.imeiSIM2 = null;

        try {
            telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getDeviceIdGemini", 0);
            telephonyInfo.imeiSIM2 = getDeviceIdBySlot(context, "getDeviceIdGemini", 1);
        } catch (GeminiMethodNotFoundException e) {
            e.printStackTrace();

            try {
                telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getDeviceId", 0);
                telephonyInfo.imeiSIM2 = getDeviceIdBySlot(context, "getDeviceId", 1);
            } catch (GeminiMethodNotFoundException e1) {
                //Call here for next manufacturer's predicted method name if you wish
                e1.printStackTrace();
            }
        }

        telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
        telephonyInfo.isSIM2Ready = false;

        try {
            telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimStateGemini", 0);
            telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimStateGemini", 1);
        } catch (GeminiMethodNotFoundException e) {

            e.printStackTrace();

            try {
                telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimState", 0);
                telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimState", 1);
            } catch (GeminiMethodNotFoundException e1) {
                //Call here for next manufacturer's predicted method name if you wish
                e1.printStackTrace();
            }
        }
    }

    return telephonyInfo;
}

private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

    String imei = null;

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try{

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);

        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        Object ob_phone = getSimID.invoke(telephony, obParameter);

        if(ob_phone != null){
            imei = ob_phone.toString();

        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeminiMethodNotFoundException(predictedMethodName);
    }

    return imei;
}

private static  boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

    boolean isReady = false;

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try{

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter);

        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);

        if(ob_phone != null){
            int simState = Integer.parseInt(ob_phone.toString());
            if(simState == TelephonyManager.SIM_STATE_READY){
                isReady = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeminiMethodNotFoundException(predictedMethodName);
    }

    return isReady;
}


private static class GeminiMethodNotFoundException extends Exception {

    private static final long serialVersionUID = -996812356902545308L;

    public GeminiMethodNotFoundException(String info) {
        super(info);
    }
}
}

然后使用这个类:

private void isDualSimOrNot(){
    SimNoInfo telephonyInfo = SimNoInfo.getInstance(this);

    String imeiSIM1 = telephonyInfo.getImeiSIM1();
    String imeiSIM2 = telephonyInfo.getImeiSIM2();

    boolean isSIM1Ready = telephonyInfo.isSIM1Ready();
    boolean isSIM2Ready = telephonyInfo.isSIM2Ready();

    boolean isDualSIM = telephonyInfo.isDualSIM();
    Log.i("Dual = "," IME1 : " + imeiSIM1 + "\n" +
            " IME2 : " + imeiSIM2 + "\n" +
            " IS DUAL SIM : " + isDualSIM + "\n" +
            " IS SIM1 READY : " + isSIM1Ready + "\n" +
            " IS SIM2 READY : " + isSIM2Ready + "\n");
}

并且不要忘记在androidmanifest中添加权限。

<uses-permission android:name="android.permission.READ_PHONE_STATE" />