慕的地8271018
我有一个带有Android4.4.4的三星Duos设备,Seetha在接受的答案(即调用getDeviceIdDs)中建议的方法不适合我,因为这个方法不存在。通过调用方法“getDefault(IntslotID)”,我能够恢复所需的所有信息,如下所示:public static void samsungTwoSims(Context context) {
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 getFirstMethod = telephonyClass.getMethod("getDefault", parameter);
Log.d(TAG, getFirstMethod.toString());
Object[] obParameter = new Object[1];
obParameter[0] = 0;
TelephonyManager first = (TelephonyManager) getFirstMethod.invoke(null, obParameter);
Log.d(TAG, "Device Id: " + first.getDeviceId() + ", device status: " + first.getSimState() + ", operator: "
+ first.getNetworkOperator() + "/" + first.getNetworkOperatorName());
obParameter[0] = 1;
TelephonyManager second = (TelephonyManager) getFirstMethod.invoke(null, obParameter);
Log.d(TAG, "Device Id: " + second.getDeviceId() + ", device status: " + second.getSimState()+ ", operator: "
+ second.getNetworkOperator() + "/" + second.getNetworkOperatorName());
} catch (Exception e) {
e.printStackTrace();
} }此外,我还重写了反复测试方法以恢复此信息的代码,以便它使用一个方法名称数组,而不是一系列try/catch。例如,要确定是否有两个活动的Sims,我们可以这样做:private static String[] simStatusMethodNames = {"getSimStateGemini", "getSimState"};public static boolean hasTwoActiveSims(Context context) {
boolean first = false, second = false;
for (String methodName: simStatusMethodNames) {
// try with sim 0 first
try {
first = getSIMStateBySlot(context, methodName, 0);
// no exception thrown, means method exists
second = getSIMStateBySlot(context, methodName, 1);
return first && second;
} catch (GeminiMethodNotFoundException e) {
// method does not exist, nothing to do but test the next
}
}
return false;}这样,如果为某个设备建议了一个新的方法名,您可以简单地将它添加到数组中,并且它应该可以工作。