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 ?.
Asked
Active
Viewed 333 times
1
-
Xamarin uses what Android and iOS uses. You don't really need anything related to xamarin, you must implement what you found for native android and native ios. The example you linked should work, but you should have a look at the IntentFilter C# attribute for Xamarin projects, rather than trying to edit the androidmanifest file yourself – Miiite Aug 20 '19 at 13:57
-
Thanks @Miiite . I would look into Intent Filter. :) – Nancy Kanwar Aug 21 '19 at 05:49
1 Answers
1
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.

Nancy Kanwar
- 157
- 2
- 16

R15
- 13,982
- 14
- 97
- 173
-
Thanks for your answer . I did same but was unable to hit this method. – Nancy Kanwar Aug 22 '19 at 06:58
-
You try to put toast msg instead of Log.Debug and check. This is working example yesterday I have implemented – R15 Aug 22 '19 at 07:03
-
-
I tested , But its not hitting breakpoint . I am testing it like "Build app" then "change sim" . Execption throws . this method is in try catch . – Nancy Kanwar Aug 22 '19 at 10:31
-
Yeah, while debugging you need to open/close SIM slot. What exception it is throwing? also can you add manifest code in question. – R15 Aug 22 '19 at 10:33
-
about manifest , I have added permission like you said . Is something else also required there ? . – Nancy Kanwar Aug 22 '19 at 10:37
-
Only this permission needed, nothing else. You need to write same code in BroadcastReceiver class , I have provided. Don't miss first two attributes. Can you tell me about exception? – R15 Aug 22 '19 at 10:38
-
Okay So I have made a slight change I have added priority with Intent Filter . Its hitting breakpoint now and my execption details are : [these](https://docs.google.com/document/d/1DWlDje_Nn9wjeLqUCXtIilqHnu8TK7Gfuz2e1RueMZY/edit?usp=sharing) – Nancy Kanwar Aug 22 '19 at 10:51
-
[IntentFilter(new[] { "android.intent.action.SIM_STATE_CHANGED"}, Priority = (int)IntentFilterPriority.HighPriority)] added this – Nancy Kanwar Aug 22 '19 at 10:51
-
Ok nice! I am not sure why getting exception, you have still toast if yes may be `Application.Context` cause issue. Try to remove toast and check. – R15 Aug 22 '19 at 10:55
-
-