Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

android - Using the native sms app to send an sms without launching itself

I want to send an SMS, but not using the SmsManager class. I want to do it with the native SMS app which is there on an Android phone.

And here is the twist : I do NOT want to launch the native app while doing it. Is there some format of intent which can send an sms directly (given the sms-body and the phone number to send it to) via the native app (without the user having to click 'send').

I googled the same, but all results and responses simply launched the native sms, waiting for user to manually send the SMS. I have seen this being implemented in some apps like 'MightyText' and wish to implement in my app as well.

Please help !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using the SmsManager will send the sms through the system but will not put it in the SMS content provider as was mentioned earlier. Hence any native messaging app will not see it.

To do so, you have to add it manually via the SMS content provider AFTER you send the message normally via SmsManager. Here's some sample code to help:

ContentValues values = new ContentValues();
values.put("address", "+12345678"); // phone number to send
values.put("date", System.currentTimeMillis()+""); 
values.put("read", "1"); // if you want to mark is as unread set to 0
values.put("type", "2"); // 2 means sent message
values.put("body", "This is my message!");

Uri uri = Uri.parse("content://sms/");
Uri rowUri = context.getContentResolver().insert(uri,values);

And that's all. After that you'll notice that it's added and the native messaging app displays it normally.

Please click "accept" answer if it works out for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...