Search code examples
xamarineventssim-card

How to detect a change in Hardware(sim Change) using Xamarin


How to access event when sim card is changed in mobile(Xamarin)? I have searched and got References in android(Java) I even tried this example But its not triggering . Can I get any link related to Xamarin ?.


Solution

  • You can use this simple code to detect SIM changes.

    Add new class file to your Android project SimStateChangedReceiver.cs

    [BroadcastReceiver(Enabled = true)]
    [IntentFilter(new[] { "android.intent.action.SIM_STATE_CHANGED"}, Priority = (int)IntentFilterPriority.HighPriority)]
    public class SimStateChangedReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent0)
        {                
           Toast.MakeText(Application.Context, "Sim state has been changed", ToastLength.Long).Show();
        }
    }
    

    Also provide READ_PHONE_STATE permission in your manifest

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />   
    

    When you change SIM in your phone OnReceive method will fire.