ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 在双卡手机中获取两个simcard操作符名称

2019-07-17 10:34:12  阅读:160  来源: 互联网

标签:android telephonymanager


我想知道当移动设备是双卡时,两张SIM卡的操作符名称.在单SIM卡中我以编程方式获得了操作符名称但是对于决斗SIM我不能尽管经过这么多的搜索和尝试.

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

编辑:

有谁知道如何获取IMEI的sim操作符名称否则我有IMEI号.

码:

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);
}
}

解决方法:

当然,您可以在22版以下的手机中获取dualsim的详细信息.仅在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;
}

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

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

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

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

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

每个手机都有自己的方法.像micromax一样有像“getCarrierNameGemni”这样的方法.别担心,我给你的代码将为你处理一切.如果它无法获得结果,它将返回null.快乐的编码!

标签:android,telephonymanager
来源: https://codeday.me/bug/20190717/1487695.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有