shankar
shankar

Reputation: 161

Event when sim card is changed

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

Upvotes: 16

Views: 26965

Answers (6)

android developer
android developer

Reputation: 116412

There is an API for this:

https://developer.android.com/reference/android/telephony/SubscriptionManager#addOnSubscriptionsChangedListener(java.util.concurrent.Executor,%20android.telephony.SubscriptionManager.OnSubscriptionsChangedListener)

Sadly only from API 30.

Example:

inline fun <reified T : Any> Context.getSystemServiceCompat(): T =
    ContextCompat.getSystemService(applicationContext, T::class.java)!!

class MainActivity : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.R)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (savedInstanceState == null)
            foo(applicationContext)
    }

    companion object {
        @RequiresApi(Build.VERSION_CODES.R)
        fun foo(context: Context) {
            val subscriptionManager = context.getSystemServiceCompat<SubscriptionManager>()
            subscriptionManager.addOnSubscriptionsChangedListener(Executors.newSingleThreadExecutor(),
                object :
                    OnSubscriptionsChangedListener() {
                    override fun onSubscriptionsChanged() {
                        super.onSubscriptionsChanged()
                        Log.d("AppLog", "onSubscriptionsChanged")
                    }
                })
        }
    }
}

Upvotes: 0

Hitesh sapra
Hitesh sapra

Reputation: 256

 @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
    }

}

Upvotes: 2

huangxin
huangxin

Reputation: 510

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.

Upvotes: 4

Christoph
Christoph

Reputation: 1525

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.

Upvotes: 20

jankovd
jankovd

Reputation: 1701

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.

Upvotes: 7

Ollie C
Ollie C

Reputation: 28509

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?

Upvotes: 10

Related Questions