前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android双待手机获取每一张SIM卡的imei

android双待手机获取每一张SIM卡的imei

作者头像
yuanyuan
发布2019-09-10 18:01:00
1.1K0
发布2019-09-10 18:01:00
举报
文章被收录于专栏:小满小满

/** * create a TelephonyInfo.java class */ import java.lang.reflect.Method;

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

public final class TelephonyInfo {

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

public String getImeiSIM1() { return imeiSIM1; }

/*public static void setImeiSIM1(String imeiSIM1) { TelephonyInfo.imeiSIM1 = imeiSIM1; }*/

public String getImeiSIM2() { return imeiSIM2; }

/*public static void setImeiSIM2(String imeiSIM2) { TelephonyInfo.imeiSIM2 = imeiSIM2; }*/

public boolean isSIM1Ready() { return isSIM1Ready; }

/*public static void setSIM1Ready(boolean isSIM1Ready) { TelephonyInfo.isSIM1Ready = isSIM1Ready; }*/

public boolean isSIM2Ready() { return isSIM2Ready; }

/*public static void setSIM2Ready(boolean isSIM2Ready) { TelephonyInfo.isSIM2Ready = isSIM2Ready; }*/

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

private TelephonyInfo() { }

public static TelephonyInfo getInstance(Context context){

if(telephonyInfo == null) {

telephonyInfo = new TelephonyInfo();

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

And then use this class like that:

代码语言:javascript
复制
private void isDualSimOrNot(){
    TelephonyInfo telephonyInfo = TelephonyInfo.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");
}

Above code works perfectly for me.

转载自:http://stackoverflow.com/questions/11880881/how-can-i-get-both-imei-numbers-from-dual-sim-mobile

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-04-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档