我正在创建一个双卡手机应用程序,该应用程序应能够检测用户拨打电话的sim卡。无论是呼出还是呼入电话都可以。我尝试使用这篇教程获取设备的两个IMEI号码,但第二个IMEI号码返回null。
无论如何,我必须检测用户在拨打或接听电话时使用的是哪张sim卡。
请建议任何实现此目标的方法。
在控制台中查看SIM1状态的类型:
adb shell dumpsys telephony.registry
要在控制台中查看SIM2类型的状态:
adb shell dumpsys telephony.registry2
mCallState
在来电/去电时会发生变化。它可以让你知道哪张 SIM 卡用于通话。
当你从 Java 应用程序中调用 dumpsys
时,你需要在清单文件中声明 android.permission.DUMP
权限。但是在一些新设备上可能无法正常工作(它们会出现“权限拒绝”错误)。
这可能会给你双卡手机的 IMEI 号码。
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();
}
}