Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event when sim card is changed

Tags:

android

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

like image 397
shankar Avatar asked Mar 22 '11 10:03

shankar


1 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.

like image 119
Christoph Avatar answered Sep 19 '22 13:09

Christoph