16

How to access event when sim card is changed in mobile?

Matthieu Harlé
  • 739
  • 1
  • 13
  • 32
shankar
  • 161
  • 1
  • 1
  • 5

5 Answers5

20

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.

Community
  • 1
  • 1
Christoph
  • 1,525
  • 1
  • 16
  • 27
10

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?

Community
  • 1
  • 1
Ollie C
  • 28,313
  • 34
  • 134
  • 217
  • 9
    It may be possible to eject SIM card without turning phone in some models. – pixel Jun 03 '11 at 08:08
  • 2
    Yes, 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
7

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.

Community
  • 1
  • 1
jankovd
  • 1,681
  • 1
  • 16
  • 22
4

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.

tomrozb
  • 25,773
  • 31
  • 101
  • 122
huangxin
  • 510
  • 5
  • 6
2
 @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
    }

}
Hitesh sapra
  • 248
  • 2
  • 12