简体   繁体   中英

Xamarin.Andriod: How to Delete or mark as read message(conversation) From Inbox

I'm trying to change that comment line to some Xamarin code for Delete Conversation or mark as reading and Remove Notification about the message received...

    public class SmsReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            if (intent.HasExtra("pdus"))
            {
                var smsArray = (Object[])intent.Extras.Get("pdus");
                string address = "";
                // string message = "";
                foreach (var item in smsArray)
                {
                    var sms = SmsMessage.CreateFromPdu((byte[])item);
                    _message = sms.MessageBody;
                    _sender = sms.OriginatingAddress;
                }
                if (_sender.Contains(txtCarNumber.Text.Substring(1, 10)))
                {
                    MsgReceived(_message, context);
                    SwitchClass.StopProgress();
                    var i = new Intent(context, typeof(NotificationsCommand));
                    i.PutExtra("Command", _message);
                    context.StartService(i);

                    //// here should be (Delete) or (Mark as read) message Conversation 

                }
            }
        }
    }

here is delete and mark as read method,you could refer to:

private void DeletMessage(Context context)
    {
        try
        {
            Uri uriSms = Uri.Parse("content://sms/inbox");
            var cursor = context.ContentResolver.Query(uriSms, new string[] { "_id", "thread_id" },
                    null, null, null);
            if (null != cursor && cursor.MoveToFirst())
            {
                do
                {
                    // Delete SMS
                    long threadId = cursor.GetLong(1);

                    int result = context.ContentResolver.Delete(Uri
                            .Parse("content://sms/conversations/" + threadId),
                            null, null);
                } while (cursor.MoveToNext());
            }
        }
        catch (Exception e)
        {
        }
    }

 private void MarkAsRead(Context context)
    {
        Uri uri = Uri.Parse("content://sms/inbox");
        var cursor = context.ContentResolver.Query(uri, null, null, null, null);
        try
        {

            while (cursor.MoveToNext())
            {
                if ((cursor.GetString(cursor.GetColumnIndex("address")).Equals(number)) && (cursor.GetInt(cursor.GetColumnIndex("read")) == 0))
                {
                    if (cursor.GetString(cursor.GetColumnIndex("body")).StartsWith(body))
                    {
                        string threadId = cursor.GetString(cursor.GetColumnIndex("_id"));
                        ContentValues values = new ContentValues();
                        values.Put("read", 1);
                        context.ContentResolver.Update(Uri.Parse("content://sms/inbox"), values, "_id=" + threadId, null);
                        return;
                    }
                }
            }
        }
        catch (Exception e)
        {
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM