I have written an app with a minimum amount of code to implement a BroadcastReceiver. The purpose of this app is for learning, also I can use this to test code in the future for capturing various IntentFilters.
The app works properly for the IntentFilter "android.intent.action.HEADSET_PLUG". It does not work for "android.provider.Telephony.SMS_RECEIVED". I have set the uses-permission for "android.permission.RECEIVE_SMS".
The code is shown below, it is short. I merely use RegisterReceiver to identify the IntentsFilters. I am not putting the section in the manifest. This works for HEADSET_PLUG, but not for SMS_RECEIVED.
I have done lots of google searches, done my homework, but am unable to get the code working to detect when SMS messages are received. If you see my mistake, please, don't just type a short reply. Could you take this small amount of code, see it fail, then make the modifications to make it work, and post back the solution. I am using Visual Studio 2012.
Thanks
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Xml.Serialization;
using System.Text;
namespace IntentWatch
{
[Activity(
Label = "IntentWatch", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
public TextView txtNote; private MyBroadcastReceiver _recevier;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
_recevier = new MyBroadcastReceiver();
RegisterReceiver(_recevier, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
RegisterReceiver(_recevier, new IntentFilter("android.intent.action.HEADSET_PLUG"));
}
}
[BroadcastReceiver]
public class MyBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(Application.Context, "OnReceive: " + intent.Action, ToastLength.Short).Show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<application></application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
</manifest>