Xamarin.Andriod:如何从收件箱中删除或标记为已读邮件(对话)

时间:2019-07-16 14:43:04

标签: c# xamarin xamarin.android

我正在尝试将该注释行更改为一些Xamarin代码以进行“删除对话”,或将其标记为已读并删除有关收到的消息的通知...

    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 

                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

这是删除并标记为已读方法,您可以参考:

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)
        {
        }
    }