我使用c#在xamarin android中进行编码,并尝试将sms从现有文件恢复到手机。我已经获取了所有的短信并将它们放入准备保存的列表中。现在的问题是,当我尝试保存它时,我收到短信保存的消息,但在我原来的消息应用程序中,我没有看到新添加的短信。我在我的函数中使用了以下代码行:
ContentValues values = new ContentValues();
values.Put(Telephony.Sms.Conversations.InterfaceConsts.Body, "Hello folks");
values.Put(Telephony.Sms.Conversations.InterfaceConsts.Address, "6879890");
// values.Put(Telephony.Sms.Conversations.InterfaceConsts.Type, 0);
values.Put(Telephony.Sms.Conversations.InterfaceConsts.Read, 1);
values.Put(Telephony.Sms.Conversations.InterfaceConsts.Date, "1513071781");
Application.Context.ApplicationContext.ContentResolver.Insert(Telephony.Sms.Inbox.ContentUri, values);
var context = Application.Context.ApplicationContext;
var values = new ContentValues();
values.Put("address", "+9868665567");
values.Put("body", "Added via Xamarin");
values.Put("read", false);
values.Put("date", "1513071781");
try
{
context.ContentResolver.Insert(Telephony.Sms.Inbox.ContentUri, values);
Toast.MakeText(this, "SMS saved ! ", ToastLength.Short).Show();
}
catch(Exception ex)
{
Toast.MakeText(this, "SMS not saved ! "+ex.Message, ToastLength.Long).Show();
}
我总是收到成功消息并且永远不会收到错误,但我永远看不到反映的变化
答案 0 :(得分:0)
在@mike的帮助下,我已经解决了问题,现在可以添加短信了。 这是最终的代码:
private void SMS_test_Click(object sender, EventArgs e)
{
var context = Application.Context.ApplicationContext;
String defaultSmsApp = Telephony.Sms.GetDefaultSmsPackage(context);
//Add this to make your app default
Intent intent = new Intent();
intent.SetAction(Telephony.Sms.Intents.ActionChangeDefault);
intent.PutExtra(Telephony.Sms.Intents.ExtraPackageName, context.PackageName);
StartActivity(intent);
//Save the message
var values = new ContentValues();
values.Put("address", "+982323855");
values.Put("body", "Added via Xamarin");
values.Put("read", false);
values.Put("date", "1513071781");
context.ContentResolver.Insert(Telephony.Sms.Sent.ContentUri, values);
Toast.MakeText(this, "Message added : ", ToastLength.Long).Show();
//Add this to revert the default app to what it previously was.
intent = new Intent();
intent.SetAction(Telephony.Sms.Intents.ActionChangeDefault);
intent.PutExtra(Telephony.Sms.Intents.ExtraPackageName, defaultSmsApp);
StartActivity(intent);
}