How to access event when sim card is changed in mobile?
-
Similar questions posted: http://stackoverflow.com/questions/8629766/android-sim-change – aggregate1166877 May 24 '12 at 11:45
5 Answers
Basically, the answer to this question "How to monitor SIM state change" is the correct answer to your question as well.
So you create a new class
package a.b.c;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class SimChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("SimChangedReceiver", "--> SIM state changed <--");
// Most likely, checking if the SIM changed could be limited to
// events where the intent's extras contains a key "ss" with value "LOADED".
// But it is more secure to just always check if there was a change.
}
}
and adapt your AndroidManifest.xml to contain
<!-- put this where your other permissions are: -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!-- and -->
<application
android:name="a.b.c...."
... >
<!-- put this somewhere into your application section: -->
<receiver android:name="a.b.c.SimChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.SIM_STATE_CHANGED"/>
</intent-filter>
</receiver>
</application>
As usual on Android there no guarantees it works on any version nor on any manufacturer's devices.
I'm not aware of an event, but the phone will be turned off and on when the SIM is swapped, so you can create a service that stores the SIM serial number in preferences, and then compare the serial number stored with that in the current SIM when the service starts.
Here's details of accessing the SIM details: Access the SIM Card with an Android Application?
-
9It may be possible to eject SIM card without turning phone in some models. – pixel Jun 03 '11 at 08:08
-
2Yes, on Galaxy Nexus, you can remove the SIM card while running. If you insert a new SIM while running, it won't take affect until you power cycle. Getting the number from the telephony manager will return null / empty string. – Stealth Rabbi Nov 30 '12 at 13:32
Save the cureent SIM ID and everytime the phone goes from state AirplaneModeON to AirplaneModeOFF check if the new SIM ID is the same as the one saved before.
Check this answer to see how to detect Airplane Mode.
I hope this answers you question.
You must register a BroadcastReceiver
to receive the action android.intent.action.SIM_STATE_CHANGED
.
This action is included in com.android.internal.telephony.TelephonyIntents.java
and cannot be found in Android's Documentation. When you've received it (e.g. sim card pluged in/out), get Sim State extra with key ss
.
@Override
public void onReceive(final Context context, final Intent intent) {
Log.d("SimChangedReceiver", "--> SIM state changed <--");
SubscriptionManager sm = SubscriptionManager.from(context);
List<SubscriptionInfo> sis = sm.getActiveSubscriptionInfoList();
SubscriptionInfo si = sis.get(0);
SubscriptionInfo si1 = sis.get(1);
String iccId = si.getIccId();
String iccId1 = si1.getIccId();
Log.e("iccId---------", iccId);
Log.e("iccId1---------", iccId1);
if (iccId.equals(oldID1)
{
//Your Code
}else if (iccIdiccId1.equals(oldID2)
{
//Your Code
}
}

- 248
- 2
- 12