我一直在开发一个用于接收SIM_STATUS_CHANGED
广播的应用程序。我注意到,每当我在应用程序运行时将SIM卡移除并重新插入设备中时,切换Airplane Mode
后再次检测到SIM卡,就会重新创建活动。
任何原因!!!这是默认行为吗?。我一直在Google上搜索,但未找到任何有关该问题的文章...
Manifest.xml
<receiver android:name="com.yego.util.SimChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.SIM_STATE_CHANGED"/>
</intent-filter>
</receiver>
SimChangedReceiver类
public class SimChangedReceiver extends BroadcastReceiver implements WaitUtil.WaitListner {
TelephonyManager telephonyManager;
SharedPrefUtil objSharedPref;
DBHelper dbHelper;
static Integer waitCount;
@Override
public void onReceive(Context context, Intent intent) {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int simState = telephonyManager.getSimState();
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
Log.e("MyApp", "Sim State absent");
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
Log.e("MyApp", "Sim State network locked");
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED:
Log.e("MyApp", "Sim State pin required");
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED:
Log.e("MyApp", "Sim State puk required");
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
Log.e("MyApp", "Sim State unknown");
break;
case TelephonyManager.SIM_STATE_READY:
try {
Log.e("MyApp", "Sim State Ready");
objSharedPref = new SharedPrefUtil(context);
if (objSharedPref.GetAppStringPrefByKey(R.string.device_sim_id_key).equals("")) { // No SIM Id is stored in preference
objSharedPref.SetAppPrefsByKey(R.string.device_sim_id_key, telephonyManager.getSimSerialNumber());
} else {
if (!objSharedPref.GetAppStringPrefByKey(R.string.device_sim_id_key).equals(telephonyManager.getSimSerialNumber())) { // saved sim id does not match curent sim id
objSharedPref.SetAppPrefsByKey(R.string.device_sim_id_key, telephonyManager.getSimSerialNumber());
dbHelper = DBHelper.getInstance(context);
Log.e("MyApp", "Executing Wait Task First Time");
WaitUtil waitUtil = new WaitUtil(this);
waitUtil.execute(1000);
waitCount = 1;
}
}
} catch (SecurityException e) {
Crashlytics.logException(e);
} catch (Exception e) {
Crashlytics.logException(e);
}
break;
}
}
public void onWaitaskCompleted(boolean status) {
try {
if ((telephonyManager.getSubscriberId() != null && !telephonyManager.getSimCountryIso().equals("") &&
!telephonyManager.getNetworkCountryIso().equals("")) || waitCount >= 20) {
GetSimInfo(telephonyManager);
} else {
Log.e("MyApp", "Executing Wait Task" + String.valueOf(waitCount));
WaitUtil waitUtil = new WaitUtil(this);
waitUtil.execute(3000);
waitCount++;
}
}catch (SecurityException e){
Crashlytics.logException(e);
}