Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to get information when an SMS is send?

$
0
0

I would like to have the information when a user receives/sends an SMS. I have created a SmsWatcher class for this purpose.

using AeroMobile.Utility.Helper;
using Android.App;
using Android.Content;
using Android.Provider;

[BroadcastReceiver]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED", "android.provider.Telephony.SMS_DELIVER" }, Priority = (int)IntentFilterPriority.HighPriority)]
public class SmsWatcher : BroadcastReceiver
{

    public override async void OnReceive(Context context, Intent intent)
    {
        if (intent.Action != null)
        {
            if (intent.Action.Equals(Telephony.Sms.Intents.SmsReceivedAction))
            {
                var msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
                foreach (var msg in msgs)
                {
                    //incoming message
                }
            }
            if (intent.Action.Equals(Telephony.Sms.Intents.SmsDeliverAction))
            {
                var msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
                foreach (var msg in msgs)
                {
                    //outgoing message
                }
            }
        }
    }

}

and I register this broadcast service in the MainActivity like below:

        //starting SMS watcher service
        var smsWatcherIntent = new Intent(ApplicationContext, typeof(SmsWatcher));
        SendBroadcast(smsWatcherIntent);

Above code perfectly works for the incoming messages but not for outgoing messages. Can you please help me to get information when a user sends SMS like the information I am getting for incoming messages?


Viewing all articles
Browse latest Browse all 204402

Trending Articles