2

I'm able to send SMS using

var smsMgr = Android.Telephony.SmsManager.Default; 
smsMgr.SendTextMessage(num, null, txt, null, null);

but this message is not shown on sent sms list.
So I tried

var values = new ContentValues(); 
values.Put("address", num); 
values.Put("body", txt); 
try 
{ 
  ContentResolver.Insert(Android.Net.Uri.Parse("content://sms/sent"), values); 
} 
catch (Exception ex) 
{
  Console.WriteLine(ex.Message);
}

having in my code

[assembly: UsesPermission(Name = "android.permission.SEND_SMS")]
[assembly: UsesPermission(Name = "android.permission.WRITE_SMS")]

But everytime I get SecurityException.
Side note: my phone has not root permission.
What can I do?

Marco
  • 56,740
  • 14
  • 129
  • 152

3 Answers3

2

Finally I discovered how to let sms appear on sent tab: I needed

[assembly: UsesPermission(Name = "android.permission.READ_SMS")]

I don't know if this is a requirement of every phone or Android version, but adding that permission everything worked properly!!

Marco
  • 56,740
  • 14
  • 129
  • 152
1
private void addSENTSMS(String address, String message) {
        try {
            ContentValues values = new ContentValues();
            values.put("address", address);
            values.put("body", message);
            getContentResolver()
                    .insert(Uri.parse("content://sms/sent"), values);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

Also make sure you are using the following permission :

<uses-permission android:name="android.permission.WRITE_SMS" />
Arpit Garg
  • 8,476
  • 6
  • 34
  • 59
  • The above code works fine in all versions and API levels. I have already used the following code snippet in many of my apps. – Arpit Garg Sep 02 '13 at 07:59
  • I should have permission, 'cause I set that in my app as you can see in the question. What's the difference (in your opinion) between `ContentResolver.Insert` and `getContentResolver().insert`? – Marco Sep 02 '13 at 09:23
  • Another question: should I have the root of my phone to use that? Is there some reason for which `WRITE_SMS` permission can't be granted? Sorry, but I can test your code only in a few hours, but not now... – Marco Sep 02 '13 at 09:25
  • @Marco both are same , but while using ContentResolver.Insert returns null most of times , so more appropriate way to query content resolver is 2nd one(code snippet shared by me). Also for writing SMS , there is no need to route the phone. You just need to provide the required permission in the manifest. – Arpit Garg Sep 02 '13 at 10:46
  • I dont have `getContentResolver()`... where do you get it? Which language are you using?!? Sorry, but honestly I don't understand... am I missing some reference? – Marco Sep 02 '13 at 12:46
  • @Marco no need. e.printStackTrace() is just to print print the details of occurred exception. Use console statement as supported in your case – Arpit Garg Sep 02 '13 at 12:47
  • m using Java based ,SDK , instead use getApplicationContext().getContentResolver() – Arpit Garg Sep 02 '13 at 12:48
  • But I'm using C#, Mono for Android!! :( – Marco Sep 02 '13 at 12:50
  • ok but I guess the syntax and dial-acts are same in either cases. :( – Arpit Garg Sep 02 '13 at 12:55
  • Syntax I used should do exactly what you do with Java... this is what I'm asking... but it doesn't work :( – Marco Sep 02 '13 at 12:56
  • Sorry but can't assist you out of your problem. It was a well working and tested code so I shared that. – Arpit Garg Sep 02 '13 at 13:16
  • Look at my answer... don't ask me the reason, but that's what I needed to let it work :( – Marco Sep 02 '13 at 13:31
  • ok nice to see gr8 work :) finally u discovered the solution. – Arpit Garg Sep 02 '13 at 13:38
  • Did you ever needed that permission to save sms on sent tab? – Marco Sep 02 '13 at 13:45
  • Actually my app was intended to perform several operation as well. So, I was already using permission.READ_SMS – Arpit Garg Sep 03 '13 at 04:47
0

why I have compilation error "The name 'getContentResolver' does not exist in the current context" for getContentResolver.Insert(...) in method implemented in class MainActivity:Activity?

constructor
  • 1,412
  • 1
  • 17
  • 34