Android Phone Manager for sim detection

I am working on Android startup, which mainly depends on the state of the SIM card. When my application starts automatically, I need to check it, where the SIM card was changed. After that, I compare the current SIM card with the previous SIM card, getting general preferences. But the application returns a null pointer exception when a new SIM value is received.

I want to respond to sim states.

When I get the SIM_STATE_READY state, I want to get the new SIM status from the phone manager.

  telMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); int simState = telMgr.getSimState(); switch (simState) { case (TelephonyManager.SIM_STATE_ABSENT): System.out.println("*******************************************Sim State absent******************************"); break; case (TelephonyManager.SIM_STATE_NETWORK_LOCKED): System.out.println("*******************************************SIM_STATE_NETWORK_LOCKED******************************"+sim); break; case (TelephonyManager.SIM_STATE_PIN_REQUIRED): System.out.println("*******************************************SIM_STATE_PIN_REQUIRED******************************"+sim); break; case (TelephonyManager.SIM_STATE_PUK_REQUIRED): System.out.println("*******************************************SIM_STATE_PUK_REQUIRED******************************"+sim); break; case (TelephonyManager.SIM_STATE_UNKNOWN): System.out.println("*******************************************SIM_STATE_UNKNOWN******************************"+sim); break; case (TelephonyManager.SIM_STATE_READY): { } break; } default: break; } 

I do this, but don’t know how to listen to the SIM state that I want when the SIM card is ready so that I can execute some kind of code. When the device boots up, it always returns "SIM_STATE_UNKNOWN" and terminates the program.

Please help me achieve this.

+6
android
source share
3 answers

If you want to respond to a change in Sim, you need to install a listener:

 final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); tm.listen(new PhoneStateListener() { @Override public void onServiceStateChanged(ServiceState serviceState) { //Code in here executed on Sim State change } @Override public void onDataConnectionStateChanged(int state) { } 
+3
source share

Are you trying to configure the application on an Android emulator? Perhaps you should try your application on an Android phone directly, I think the android emulator cannot support your application.

+1
source share

Below code will help you choose SIM Serial No

 TelephonyManager mTelephonyMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); String sSimSerial = mTelephonyMgr.getSimSerialNumber(); 

Set the following permission in the Android manifest file

android.permission.READ_PHONE_STATE

0
source share

All Articles