所以这就是场景。
当我第一次测试我使用的应用程序时 getSimSerialNumber()函数,我成功将我的SIM号码设为“89912000107679766XYZ”。
现在,每当我运行相同的功能时,无论飞行模式是打开还是关闭,我都会获得Null值。
我尝试使用getSimState()进行检查,并且总是得到Constant值5,即“SIM_STATE_READY”。
问题究竟在哪里?为什么我现在不能获得正确的SimSerialNumber?
编辑1:
参考代码库:
public void doSimStateCheck(Context c){
String savedSimCardNo = ClientConfig.readSimCardNumber(c); // I have saved device's sim card somewhere here.
String existingSimCardNo = "";
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String simID = "";
boolean airPlaneMode = isAirplaneModeOn(c);
if(airPlaneMode){
// toggle airplane mode
setAirplaneModeOn(c,0); //Setting Airplane mode on - this might not work as stated for Jellybean and above. I have given WRITE_SETTINGS and WRITE_SECURE_SETTINGS permission already. I am not worried about this piece of code anyways.
simID = tm.getSimSerialNumber();
setAirplaneModeOn(c, 1); //Setting Airplane mode back to off.
}
else{
simID = tm.getSimSerialNumber(); // I am getting null here, i.e. when Airplane mode is NOT on, still I get null :( THIS IS MY CONCERN.
}
if (simID != null)
existingSimCardNo = simID;
else{
Log.v("SimState",""+tm.getSimState()); // This always returns 5.
existingSimCardNo = "NULL";
}
Log.v("SavedSimCardState", "It is " + savedSimCardNo); //This returns 89912000107679766XYZ
Log.v("ExistingSimCardState","It is "+existingSimCardNo); //This shows NULL as stated above.
if(!savedSimCardNo.equals(existingSimCardNo)){
//do stuff when sim card numbers are not same as saved earlier.
}
}
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isAirplaneModeOn(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
} else {
return Settings.Global.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void setAirplaneModeOn(Context context,int state) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, state);
} else {
Settings.Global.putInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, state);
}
}