[英]Message not added to store after SmsManager SendTextMessage
G'day,天,
My question is fairly straight forward:我的问题很简单:
Should I expect SMSManager.Default.SendTextMessage()
to automatically add the sent message to the message store, or do I need to add it myself?我应该期望SMSManager.Default.SendTextMessage()
自动将发送的消息添加到消息存储中,还是我需要自己添加? If I need to add it myself, should I do that in the BroadcastReceiver
or in the dependency service after sending?如果我需要自己添加,我应该在发送后在BroadcastReceiver
中还是在依赖服务中添加?
For context:对于上下文:
I'm trying to recreate an SMS app as practice for myself.我正在尝试重新创建一个 SMS 应用程序作为自己的练习。
I've managed to create a dependency service that gets messages from the message store and have a BroadcastReceiver
to notify when a message is received.我设法创建了一个依赖服务,它从消息存储中获取消息,并有一个BroadcastReceiver
来通知何时收到消息。
My issue arises when I try to use the below code to send a message.当我尝试使用以下代码发送消息时,出现了我的问题。
public Task SendSMS(string message, string destination)
{
piSent = PendingIntent.GetBroadcast(context, destination.GetHashCode(), new Intent("SMS_SENT"), PendingIntentFlags.UpdateCurrent);
//var piDelivered = PendingIntent.GetBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
try
{
manager.SendTextMessage(destination, null, message, piSent, null);
}
catch (Exception e)
{
Toast.Make(e.Message, ToastDuration.Long).Show();
}
return Task.CompletedTask;
}
So the BroadcastReceiver
for SMS_SENT gets the intent and has a Result.Ok
code, but the message itself isn't added to the message store.因此 SMS_SENT 的BroadcastReceiver
获取意图并具有Result.Ok
代码,但消息本身并未添加到消息存储中。 I also haven't live tested this to see if the message actually sends.我也没有对此进行现场测试以查看消息是否实际发送。
This is the code I use to get all messages from the store.这是我用来从商店获取所有消息的代码。
public async Task<ObservableCollection<Message>> GetMessages(string thread_id)
{
var inbox = App.Current.Resources["smsInbox"] as string;
var cols = new string[] { "address", "_id", "thread_id", "body", "date" };
var projection = "thread_id=?";
string[] selection = { thread_id };
var results = await ExecuteQuery(inbox, cols, projection, selection);
var messages = new ObservableCollection<Message>();
foreach(var result in results)
{
messages.Add(new Message
{
Address = result[0],
Id = result[1],
ThreadId = result[2],
Body = result[3],
Date = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(result[4]))
});
}
return messages;
}
private async Task<List<List<string>>> ExecuteQuery(string uriString, string[] cols, string projection, string[] projectionValue = null)
{
await init();
var queryData = new List<List<string>>();
var uri = Android.Net.Uri.Parse(uriString);
var cursor = context.ContentResolver.Query(uri, cols, projection, projectionValue, null);
if (cursor.MoveToFirst())
{
do
{
var tuple = new List<string>(cursor.ColumnCount);
for (int i = 0; i < cursor.ColumnCount; i++)
{
tuple.Add(cursor.GetString(i));
}
queryData.Add(tuple);
} while (cursor.MoveToNext());
}
cursor.Close();
return queryData;
}
And the broadcastreceiver和广播接收器
[BroadcastReceiver(Exported = true)]
public class MessageSentReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
switch ((int)ResultCode)
{
case (int)Result.Ok:
Toast.Make("SMS Sent", ToastDuration.Short).Show();
break;
case (int)SmsResultError.GenericFailure:
Toast.Make("Generic Error", ToastDuration.Short).Show();
break;
case (int)SmsResultError.NoService:
Toast.Make("No Service", ToastDuration.Short).Show();
break;
case (int)SmsResultError.NullPdu:
Toast.Make("Null PDU", ToastDuration.Short).Show();
break;
case (int)SmsResultError.RadioOff:
Toast.Make("Radio Off", ToastDuration.Short).Show();
break;
default:
Toast.Make("Default Message", ToastDuration.Short).Show();
break;
}
}
}
Thanks in advance, and apologies if this isn't enough information to answer the question.提前致谢,如果这些信息不足以回答问题,我们深表歉意。
Well, after double-triple checking I wasn't making a fool of myself, turns out I was.好吧,经过两次三重检查后,我并没有在自欺欺人,事实证明我是在自欺欺人。
The messages are saved in the sent table, so weren't showing up when I pulled from the inbox.消息保存在已发送表中,因此当我从收件箱中提取时没有显示。
Lesson learnt!学习到教训了!
I guess I'll leave this here to save someone else that's having a brain fade.我想我会把它留在这里,以拯救正在脑退化的其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.