我有一個帶有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;}
這樣,如果為某個設備建議了一個新的方法名,您可以簡單地將它添加到數組中,并且它應該可以工作。