android phone app 多个sim,Android:检查手机是否是双SIM

这篇博客介绍了如何通过Java反射技术在Android应用中获取手机的两个IMEI号码,以此来判断设备是否为双SIM卡。代码示例展示了如何使用TelephonyManager类及其反射方法来获取SIM卡状态和IMEI信息,特别处理了不同制造商可能的定制方法名称。
摘要由CSDN通过智能技术生成

其他可能的选择:

你可以用Java反射得到两个IMEI号码。

使用这些IMEI号码,您可以检查手机是否是双SIM。

尝试以下活动:import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this);

String imeiSIM1 = telephonyInfo.getImsiSIM1();

String imeiSIM2 = telephonyInfo.getImsiSIM2();

boolean isSIM1Ready = telephonyInfo.isSIM1Ready();

boolean isSIM2Ready = telephonyInfo.isSIM2Ready();

boolean isDualSIM = telephonyInfo.isDualSIM();

TextView tv = (TextView) findViewById(R.id.tv);

tv.setText(" IME1 : " + imeiSIM1 + "\n" +

" IME2 : " + imeiSIM2 + "\n" +

" IS DUAL SIM : " + isDualSIM + "\n" +

" IS SIM1 READY : " + isSIM1Ready + "\n" +

" IS SIM2 READY : " + isSIM2Ready + "\n");

}}

下面是TelephonyInfo.java :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 getImsiSIM1() {

return imeiSIM1;

}

/*public static void setImsiSIM1(String imeiSIM1) {

TelephonyInfo.imeiSIM1 = imeiSIM1;

}*/

public String getImsiSIM2() {

return imeiSIM2;

}

/*public static void setImsiSIM2(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);

}

}}

编辑:

对于其他SIM插槽的细节,获得诸如“getDeviceIdGemini”之类的方法的访问权限可以预测方法的存在。

如果该方法的名称与设备制造商给出的名称不匹配,则该方法将无法工作。您必须为这些设备找到相应的方法名。

可以使用Java反射为其他制造商查找方法名称,如下所示:public static void printTelephonyManagerMethodNamesForThisDevice(Context context) {

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

Class> telephonyClass;

try {

telephonyClass = Class.forName(telephony.getClass().getName());

Method[] methods = telephonyClass.getMethods();

for (int idx = 0; idx 

System.out.println("\n" + methods[idx] + " declared by " + methods[idx].getDeclaringClass());

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

}}

编辑:

如塞塔她在评论中指出:telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getDeviceIdDs", 0);telephonyInfo.imeiSIM2 =

getDeviceIdBySlot(context, "getDeviceIdDs", 1);

这对她很管用。她成功地为三星Duos设备中的SIM获得了两个IMEI号码。

编辑2:

用于获取数据的方法是用于联想A 319和该制造商的其他手机(Creditor)。Maher Abuthraa):telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getSimSerialNumberGemini", 0); telephonyInfo

.imeiSIM2 = getDeviceIdBySlot(context, "getSimSerialNumberGemini", 1);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值