I am created BroadcastReceiver in ListView but not working.this is my code

AnbuMani27AnbuMani27 USMember ✭✭✭

XAML:

SmsReceiver Class:

[BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]

public class SmsReceiver : BroadcastReceiver
{
private const string Tag = "SMSBroadcastReceiver";
private const string IntentAction = "android.provider.Telephony.SMS_RECEIVED";
public override void OnReceive(Context context, Intent intent)
{
Log.Info(Tag, "Intent received: " + intent.Action);
if (intent.Action != IntentAction) return;
var bundle = intent.Extras;
if (bundle == null) return;
var pdus = bundle.Get("pdus");
var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
var msgs = new SmsMessage[castedPdus.Length];
var sb = new StringBuilder();
for (var i = 0; i < msgs.Length; i++)
{
var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
JNIEnv.CopyArray(castedPdus[i].Handle, bytes);
msgs[i] = SmsMessage.CreateFromPdu(bytes);
sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress,
Environment.NewLine, msgs[i].MessageBody));
}
Toast.MakeText(context, sb.ToString(), ToastLength.Long).Show();
SmshomeActivity msg=SmshomeActivity.Instance();
msg.updateList(sb.ToString());
}
}

Activity:

private static SmshomeActivity inst;
ArrayAdapter arrayAdapter;
List smsMessagesList = new List();
ListView smsListView;

public static SmshomeActivity Instance()
{
return inst;
}

protected override void OnStart()
{
base.OnStart();
Log.Debug(LogTag, "SmsReceiver");
inst = this;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.sms);
// Create your application here
smsListView = (ListView)FindViewById(Resource.Id.txtlistsms);
arrayAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, smsMessagesList);
smsListView.SetAdapter(arrayAdapter);
smsListView.ItemClick += SmsListView_ItemClick;
}
private void SmsListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var a = smsMessagesList[e.Position].FirstOrDefault();
}

public void updateList(string smsMessage)
{
arrayAdapter.Insert(smsMessage, 0);
arrayAdapter.NotifyDataSetChanged();
}

Best Answer

Answers

Sign In or Register to comment.