在双SIM卡手机中同时获取两个simcard运营商名称 [英] get both simcard operator name in dual SIM mobile

查看:74
本文介绍了在双SIM卡手机中同时获取两个simcard运营商名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道手机为双SIM卡时两个SIM卡的运营商名称.在单SIM卡中,我以编程方式获得了运营商名称.

如果我在双SIM卡手机中运行我的应用程序,则我可以在应用程序中同时获得两个SIM卡运营商名称.例如:Idea,Vodafone.

有人知道如何获取IMEI的SIM运营商名称吗,然后我有IMEI号码.

代码:

 公共类MainActivity扩展了Activity {按钮btnOne,btnTwo;TextView tvInfo;上下文上下文;@Override受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnOne =(按钮)findViewById(R.id.btnOne);btnTwo =(按钮)findViewById(R.id.btnTwo);tvInfo =(TextView)findViewById(R.id.tvInfo);TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this);boolean isDualSIM = telephonyInfo.isDualSIM();boolean isSIM1Ready = telephonyInfo.isSIM1Ready();boolean isSIM2Ready = telephonyInfo.isSIM2Ready();TelephonyManager管理器=(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);尝试 {telephonyInfo.imsiSIM1 = telephonyInfo.getDeviceIdBySlot(上下文,"getSimSerialNumberGemini",0);telephonyInfo.imsiSIM2 = telephonyInfo.getDeviceIdBySlot(上下文,"getSimSerialNumberGemini",1);} catch(Exception e){//TODO自动生成的catch块e.printStackTrace();}字符串编号= manager.getLine1Number();字符串optName1 = getOutput(getApplicationContext(),"getCarrierName",0);字符串optName2 = getOutput(getApplicationContext(),"getCarrierName",1);最后的字符串carrierName = manager.getSimOperatorName();tvInfo.setText(" + isDualSIM +" + optName1 +" + optName2 +"+ telephonyInfo.imsiSIM1 +" + telephonyInfo.imsiSIM2 +"+数字+" + isSIM1Ready +" + isSIM2Ready);btnOne.setOnClickListener(new OnClickListener(){@Override公共无效onClick(查看arg0){意向意向=新意向(设置(ACTION_ACCESSIBILITY_SETTINGS);startActivity(intent);}});btnTwo.setOnClickListener(new OnClickListener(){@Override公共无效onClick(查看arg0){如果(carrierName.equalsIgnoreCase("TATA DOCOMO")||carrierName.contains("DOCOMO")){startService(new Intent(MainActivity.this,USSD.class));字符串ussdCode ="*" +"111" + Uri.encode(#");startActivity(new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + ussdCode)));}否则,如果(carrierName.equalsIgnoreCase(!dea")||carrierName.contains("idea")){startService(new Intent(MainActivity.this,USSD.class));字符串ussdCode ="*" +"121" + Uri.encode(#");startActivity(new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + ussdCode)));}否则,如果(carrierName.equalsIgnoreCase("AIRTEL")||carrierName.contains("airtel")){startService(new Intent(MainActivity.this,USSD.class));字符串ussdCode ="*" +"123" + Uri.encode(#");startActivity(new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + ussdCode)));}}});}私有静态String getOutput(Context context,String methodName,int slotId){TelephonyManager telephony =(TelephonyManager)上下文.getSystemService(Context.TELEPHONY_SERVICE);类<?>电话类;字符串ReflectionMethod = null;字符串输出= null;尝试 {telephonyClass = Class.forName(telephony.getClass().getName());for(Method method:telephonyClass.getMethods()){字符串名称= method.getName();如果(name.contains(methodName)){类<?> []参数= method.getParameterTypes();if(params.length == 1&& params [0] .getName().equals("int")){ReflectionMethod =名称;}}}} catch(ClassNotFoundException e){e.printStackTrace();}如果(reflectionMethod!= null){尝试 {输出= getOpByReflection(电话,reflectionMethod,slotId,错误的);} catch(Exception e){e.printStackTrace();}}返回输出;}私有静态字符串getOpByReflection(TelephonyManager电话,字符串预测方法名称,整数slotID,布尔值isPrivate){//Log.i("Reflection","Method:" + ForecastedMethodName +" + slotID);字符串结果= null;尝试 {类<?>telephonyClass = Class.forName(telephony.getClass().getName());Class<?> []参数= new Class [1];参数[0] = int.class;方法getSimID;如果(slotID!= -1){如果(isPrivate){getSimID = telephonyClass.getDeclaredMethod(预测方法名称,参数);} 别的 {getSimID = telephonyClass.getMethod(predictedMethodName,范围);}} 别的 {如果(isPrivate){getSimID = telephonyClass.getDeclaredMethod(predictedMethodName);} 别的 {getSimID = telephonyClass.getMethod(predictedMethodName);}}对象ob_phone;Object [] obParameter = new Object [1];obParameter [0] = slotID;如果(getSimID!= null){如果(slotID!= -1){ob_phone = getSimID.invoke(电话,obParameter);} 别的 {ob_phone = getSimID.invoke(电话);}如果(ob_phone!= null){结果= ob_phone.toString();}}} catch(Exception e){e.printStackTrace();//Log.i("Reflection","Result:" + e.printStackTrace());返回null;}返回结果;}@Overridepublic boolean onCreateOptionsMenu(Menu menu){//膨胀菜单;这会将项目添加到操作栏(如果有).getMenuInflater().inflate(R.menu.main,menu);返回true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item){//单击操作栏项.动作栏将//自动处理主页"/向上"按钮上的点击,//当您在AndroidManifest.xml中指定父活动时.int id = item.getItemId();如果(id == R.id.action_settings){返回true;}返回super.onOptionsItemSelected(item);}} 

解决方案

当然,您可以在22版以下的手机中获取dualsim的详细信息.仅在22版之后才正式受支持.

 私有静态字符串getOutput(Context context,String methodName,int slotId){TelephonyManager telephony =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);类<?>电话类;字符串ReflectionMethod = null;字符串输出= null;尝试 {telephonyClass = Class.forName(telephony.getClass().getName());for(Method method:telephonyClass.getMethods()){字符串名称= method.getName();如果(name.contains(methodName)){类<?> []参数= method.getParameterTypes();if(params.length == 1&& params [0] .getName().equals("int")){ReflectionMethod =名称;}}}} catch(ClassNotFoundException e){e.printStackTrace();}如果(reflectionMethod!= null){尝试 {输出= getOpByReflection(电话,reflectionMethod,slotId,false);} catch(Exception e){e.printStackTrace();}}返回输出;}私有静态字符串getOpByReflection(TelephonyManager电话,字符串预测方法名称,整数slotID,布尔值isPrivate){//Log.i("Reflection," Method:"+ ForecastedMethodName +""+ slotID);字符串结果= null;尝试 {类<?>telephonyClass = Class.forName(telephony.getClass().getName());Class<?> []参数= new Class [1];参数[0] = int.class;方法getSimID;如果(slotID!= -1){如果(isPrivate){getSimID = telephonyClass.getDeclaredMethod(predictedMethodName,参数);} 别的 {getSimID = telephonyClass.getMethod(predictedMethodName,参数);}} 别的 {如果(isPrivate){getSimID = telephonyClass.getDeclaredMethod(predictedMethodName);} 别的 {getSimID = telephonyClass.getMethod(predictedMethodName);}}对象ob_phone;Object [] obParameter = new Object [1];obParameter [0] = slotID;如果(getSimID!= null){如果(slotID!= -1){ob_phone = getSimID.invoke(电话,obParameter);} 别的 {ob_phone = getSimID.invoke(电话);}如果(ob_phone!= null){结果= ob_phone.toString();}}} catch(Exception e){//e.printStackTrace();返回null;}//Log.i("Reflection," Result:"+ result);返回结果;} 

使用这两种方法.您必须使用 Java反射来获取所有SIM卡详细信息.

现在只需使用一行代码即可获得所需的详细信息.

  String optName = getOutput(context,"getCarrierName",0); 

第一个参数是上下文.第二个参数是您要访问的方法名称,第三个参数是slotId."0"表示sim 1 .

此方法的所有结果均为字符串.根据您的要求进行转换需要.

每个手机都有自己的方法.像micromax一样具有"getCarrierNameGemni"之类的方法.不用担心,我给您的代码将为您处理所有事情.如果无法获得结果,则将返回null.编码愉快!

I want to know Both sim card's operator name when mobile is dual sim.In single SIM I got operator name programmatically But For duel SIM I can't although after so many search and try.

If I run My app in dual sim phone than I can get both sim card operator name in my app For example : Idea,Vodafone.

EDIT:

Is anyone know How to get sim operator name of IMEI no then I have IMEI no.

Code:

public class MainActivity extends Activity {

Button btnOne, btnTwo;
TextView tvInfo;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnOne = (Button) findViewById(R.id.btnOne);
    btnTwo = (Button) findViewById(R.id.btnTwo);
    tvInfo = (TextView) findViewById(R.id.tvInfo);

    TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this);

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

    TelephonyManager manager = (TelephonyManager) getApplicationContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    try {
        telephonyInfo.imsiSIM1 = telephonyInfo.getDeviceIdBySlot(context,
                "getSimSerialNumberGemini", 0);
        telephonyInfo.imsiSIM2 = telephonyInfo.getDeviceIdBySlot(context,
                "getSimSerialNumberGemini", 1);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String number = manager.getLine1Number();

    String optName1 = getOutput(getApplicationContext(), "getCarrierName", 0);
    String optName2 = getOutput(getApplicationContext(), "getCarrierName", 1);

    final String carrierName = manager.getSimOperatorName();
    tvInfo.setText(" " + isDualSIM + " " + optName1 + " " + optName2 + " "
            + telephonyInfo.imsiSIM1 + " " + telephonyInfo.imsiSIM2 + " "
            + number + " " + isSIM1Ready + " " + isSIM2Ready);

    btnOne.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(
                    Settings.ACTION_ACCESSIBILITY_SETTINGS);
            startActivity(intent);

        }
    });

    btnTwo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if (carrierName.equalsIgnoreCase("TATA DOCOMO")
                    || carrierName.contains("DOCOMO")) {
                startService(new Intent(MainActivity.this, USSD.class));
                String ussdCode = "*" + "111" + Uri.encode("#");
                startActivity(new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:" + ussdCode)));
            } else if (carrierName.equalsIgnoreCase("!dea")
                    || carrierName.contains("idea")) {
                startService(new Intent(MainActivity.this, USSD.class));
                String ussdCode = "*" + "121" + Uri.encode("#");
                startActivity(new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:" + ussdCode)));
            } else if (carrierName.equalsIgnoreCase("AIRTEL")
                    || carrierName.contains("airtel")) {
                startService(new Intent(MainActivity.this, USSD.class));
                String ussdCode = "*" + "123" + Uri.encode("#");
                startActivity(new Intent(Intent.ACTION_CALL, Uri
                        .parse("tel:" + ussdCode)));
            }

        }
    });

}

private static String getOutput(Context context, String methodName,
        int slotId) {
    TelephonyManager telephony = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    Class<?> telephonyClass;
    String reflectionMethod = null;
    String output = null;
    try {
        telephonyClass = Class.forName(telephony.getClass().getName());
        for (Method method : telephonyClass.getMethods()) {
            String name = method.getName();
            if (name.contains(methodName)) {
                Class<?>[] params = method.getParameterTypes();
                if (params.length == 1 && params[0].getName().equals("int")) {
                    reflectionMethod = name;
                }
            }
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    if (reflectionMethod != null) {
        try {
            output = getOpByReflection(telephony, reflectionMethod, slotId,
                    false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return output;
}

private static String getOpByReflection(TelephonyManager telephony,
        String predictedMethodName, int slotID, boolean isPrivate) {

    // Log.i("Reflection", "Method: " + predictedMethodName+" "+slotID);
    String result = null;

    try {

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

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID;
        if (slotID != -1) {
            if (isPrivate) {
                getSimID = telephonyClass.getDeclaredMethod(
                        predictedMethodName, parameter);
            } else {
                getSimID = telephonyClass.getMethod(predictedMethodName,
                        parameter);
            }
        } else {
            if (isPrivate) {
                getSimID = telephonyClass
                        .getDeclaredMethod(predictedMethodName);
            } else {
                getSimID = telephonyClass.getMethod(predictedMethodName);
            }
        }

        Object ob_phone;
        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        if (getSimID != null) {
            if (slotID != -1) {
                ob_phone = getSimID.invoke(telephony, obParameter);
            } else {
                ob_phone = getSimID.invoke(telephony);
            }

            if (ob_phone != null) {
                result = ob_phone.toString();
            }
        }
    } catch (Exception e) {
         e.printStackTrace();
        // Log.i("Reflection", "Result: " +  e.printStackTrace());
         return null;
    }

    return result;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

解决方案

Ofcourse you can get the details of dualsim in mobiles below version 22. It is only not officially supported only after 22.

private static String getOutput(Context context, String methodName, int slotId) {
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    Class<?> telephonyClass;
    String reflectionMethod = null;
    String output = null;
    try {
        telephonyClass = Class.forName(telephony.getClass().getName());
        for (Method method : telephonyClass.getMethods()) {
            String name = method.getName();
            if (name.contains(methodName)) {
                Class<?>[] params = method.getParameterTypes();
                if (params.length == 1 && params[0].getName().equals("int")) {
                    reflectionMethod = name;
                }
            }
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    if (reflectionMethod != null) {
        try {
            output = getOpByReflection(telephony, reflectionMethod, slotId, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return output;
}

private static String getOpByReflection(TelephonyManager telephony, String predictedMethodName, int slotID, boolean isPrivate) {

    //Log.i("Reflection", "Method: " + predictedMethodName+" "+slotID);
    String result = null;

    try {

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

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID;
        if (slotID != -1) {
            if (isPrivate) {
                getSimID = telephonyClass.getDeclaredMethod(predictedMethodName, parameter);
            } else {
                getSimID = telephonyClass.getMethod(predictedMethodName, parameter);
            }
        } else {
            if (isPrivate) {
                getSimID = telephonyClass.getDeclaredMethod(predictedMethodName);
            } else {
                getSimID = telephonyClass.getMethod(predictedMethodName);
            }
        }

        Object ob_phone;
        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        if (getSimID != null) {
            if (slotID != -1) {
                ob_phone = getSimID.invoke(telephony, obParameter);
            } else {
                ob_phone = getSimID.invoke(telephony);
            }

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

            }
        }
    } catch (Exception e) {
        //e.printStackTrace();
        return null;
    }
    //Log.i("Reflection", "Result: " + result);
    return result;
}

Use this two methods. You have to get all sim details by using Java reflection.

Now get the detail you want by just using a single line of code.

String optName = getOutput(context, "getCarrierName", 0);

the first param is the context. second param is the method name you wanna access and the third param is the slotId. "0" means sim 1.

All the result of this method will be string. Convert them as per your need.

Each mobile having its own method. Like micromax is having method like "getCarrierNameGemni". Don't worry, The code I gave you will handle everything for you. If it cant get the result, it will return null. Happy coding!

这篇关于在双SIM卡手机中同时获取两个simcard运营商名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆