Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,813,836 Members 42,115 Now Online
XDA Developers Android and Mobile Development Forum

[LIBRARY] Android SMS and MMS library

Tip us?
 
klinkdawg
Old
(Last edited by klinkdawg; 28th August 2013 at 10:57 PM.)
#1  
Senior Member - OP
Thanks Meter 1,737
Posts: 572
Join Date: Jul 2010

 
DONATE TO ME
Default [LIBRARY] Android SMS and MMS library

Hey guys, I put together a library to help anyone out there who is making an sms app after all my experience with Sliding Messaging and trying to get everything to work... I dug through way too much source code so hopefully this will save some others the trouble! Apologize if this isn't in the right spot mods!

Android SMS/MMS/Google Voice Sending Library

These are the APIs that Google has so far left out of the Android echosystem for easily sending any type of message without digging through source code and what not.

This library is still in BETA and has a long way to go... APIs may not be final and things will most likely change.

If you've got a better way to do things, send me a pull request! The library was created specifically for Sliding Messaging Pro and some things work the way they do specifically for that app.

-------------------------------------------

Library Overview

Sending messages is very easy to do.

First, create a settings object with all of your required information for what you want to do. If you don't set something, then it will just be set to a default and that feature may not work. For example, if you need MMS, set the MMSC, proxy, and port, or else you will get an error every time.

Code:
Settings sendSettings = new Settings();

sendSettings.setMmsc("http://mmsc.cingular.com");
sendSettings.setProxy("66.209.11.33");
sendSettings.setPort("80");
sendSettings.setGroup(true);
sendSettings.setWifiMmsFix(true);
sendSettings.setPreferVoice(false);
sendSettings.setDeliveryReports(false);
sendSettings.setSplit(false);
sendSettings.setSplitCounter(false);
sendSettings.setStripUnicode(false);
sendSettings.setSignature("");
sendSettings.setSendLongAsMms(true);
sendSettings.setSendLongAsMmsAfter(3);
sendSettings.setAccount("jklinker1@gmail.com");
sendSettings.setRnrSe(null);
- MMSC - the URL of your mms provider, found on your phone's APN settings page
- Proxy - more mms information that needs to be set for more providers to send
- Port - again, more mms stuff
- Group - whether you want to send message to multiple senders as an MMS group message or separate SMS/Voice messages
- WiFi MMS Fix - will disable wifi to send the message, only way I've found to do it, so if you can find the problem, submit a pull request
- Prefer Voice - send through Google Voice instead of SMS
- Delivery Reports - request reports for when SMS has been delivered
- Split - splits SMS messages when sent if they are longer than 160 characters
- Split Counter - attaches a split counter to message, ex. (1/3) in front of each message
- Strip Unicode - converts Unicode characters to GSM compatible characters
- Signature - signature to attach at the end of messages
- Send Long as MMS - when a message is a certain length, it is sent as MMS instead of SMS
- Send Long as MMS After - length to convert the long SMS into an MMS
- Account - this is the email address of the account that you want to send google voice messages from
- RnrSe - this is a weird token that google requires to send the message, nullifying it will make the library find the token every time, I'll hit later how to save the token and save your users some data down below in the Google Voice section.

Next, attach that settings object to the sender

Code:
Transaction sendTransaction = new Transaction(mContext, sendSettings);
Now, create the Message you want to send

Code:
Message mMessage = new Message(textToSend, addressToSendTo);
mMessage.setImage(mBitmap);
And then all you have to do is send the message

Code:
sendTransaction.sendNewMessage(message, threadId)
Note: threadId can be nullified, but this sometimes results in a new thread being created instead of the message being added to an existing thread

That's it, you're done sending

You'll also need to register a few receivers for when the messages have been sent and for delivery reports to mark them as read... In your manifest, add these lines:

Code:
<receiver android:name="com.klinker.android.send_message.SentReceiver" >
    <intent-filter>
        <action android:name="com.klinker.android.send_message.SMS_SENT" />
    </intent-filter> 
</receiver>

<receiver android:name="com.klinker.android.send_message.DeliveredReceiver" >
    <intent-filter>
                <action android:name="com.klinker.android.send_message.SMS_DELIVERED" />
    </intent-filter> 
</receiver>
Lastly, you'll need to include permissions in your manifest depending on what you want to do. Here are all of them:

Code:
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.provider.Telephony.SMS_RECEIVED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Google Voice Overview

To be able to send Google Voice messages, all you really need to do is add the final 3 permissions above and get the address of the Account that you want to send through.

To get a list of accounts available on the device, you can use the following:

Code:
ArrayList<Account> accounts = new ArrayList<Account>();
for (Account account : AccountManager.get(context).getAccountsByType("com.google")) {
    accounts.add(account);
}
Display those in a list and let the user choose which one they want to use and save that choice to your SharedPreferences.

Next, when you are configuring your send settings, you should register a receiver that listens for the action "com.klinker.android.send_message.RNRSE" like so:

Code:
if (sendSettings.getAccount() != null && sendSettings.getRnrSe() == null) {
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @override
        public void onReceive(Context context, Intent intent) {
            sharedPrefs.edit().putString("voice_rnrse", intent.getStringExtra("_rnr_se")).commit();
        }
    };

    context.registerReceiver(receiver, new IntentFilter("com.klinker.android.send_message.RNRSE"));
}
That code will then save the RnrSe value so that I don't have to fetch it every time and waste time and data. After it is saved, just insert that value into the send settings instead and you are good to go.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

And thats it, pretty simple to do, especially for MMS! There are some problems with the library at this point, so i'm really hoping someone out there who knows more about this stuff can help me out eventually and get everything cleaned up! I feel like this should be an important part of the Android APIs, yet it is all missing so this is my go at it!

GitHub
The Following 8 Users Say Thank You to klinkdawg For This Useful Post: [ Click to Expand ]
 
klinkdawg
Old
#2  
Senior Member - OP
Thanks Meter 1,737
Posts: 572
Join Date: Jul 2010

 
DONATE TO ME
This is my little way of giving back to xda and so I hope someone out there finds this and trys it out
 
klinkdawg
Old
#4  
Senior Member - OP
Thanks Meter 1,737
Posts: 572
Join Date: Jul 2010

 
DONATE TO ME
Quote:
Originally Posted by nikwen View Post
Have you got a link to your library?

Great job.
Haha oops, that might be helpful https://github.com/klinker41/android-smsmms
The Following User Says Thank You to klinkdawg For This Useful Post: [ Click to Expand ]
 
deanwray
Old
#5  
deanwray's Avatar
Senior Member
Thanks Meter 418
Posts: 1,121
Join Date: Apr 2006

 
DONATE TO ME
Quote:
Originally Posted by klinkdawg View Post
Haha oops, that might be helpful https://github.com/klinker41/android-smsmms
Must admit I have not looked much into mms at the moment, but what differs from the AOSP source ? The models look similar (not that I have looked in a while)

I always thought that sliding was built on top/over AOSP with additions rather than changes ?

forgive my "uninformed question"
 
klinkdawg
Old
(Last edited by klinkdawg; 27th August 2013 at 10:22 PM.)
#6  
Senior Member - OP
Thanks Meter 1,737
Posts: 572
Join Date: Jul 2010

 
DONATE TO ME
Quote:
Originally Posted by deanwray View Post
Must admit I have not looked much into mms at the moment, but what differs from the AOSP source ? The models look similar (not that I have looked in a while)

I always thought that sliding was built on top/over AOSP with additions rather than changes ?

forgive my "uninformed question"
Haha no, I built sliding from the ground up off of nothing, just referenced stock source when I needed to. Before I made this library, you had to download and import all kinds of internal classes not in the APIs, both from the stock app and just android source in general. This library brings all of those together so that you don't have to download hundreds of files to get the imports all sorted out (that took me days itself to do) and then it just allows you to use all those classes in a much easier way, really by only creating a message object and sending it through the transaction class.

Basically, it does everything exactly the same as what you will see from the stock source, but it encapsulates all that so third party developers have much easier access to it and can worry about more important things in their app then sifting through source code to find out how to send a stupid MMS message ha. All of the MMS source can be sooooo confusing, especially if you're just starting out like I was. :P
 
deanwray
Old
#7  
deanwray's Avatar
Senior Member
Thanks Meter 418
Posts: 1,121
Join Date: Apr 2006

 
DONATE TO ME
Quote:
Originally Posted by klinkdawg View Post
Haha no, I built sliding from the ground up off of nothing, just referenced stock source when I needed to. Before I made this library, you had to download and import all kinds of internal classes not in the APIs, both from the stock app and just android source in general. This library brings all of those together so that you don't have to download hundreds of files to get the imports all sorted out (that took me days itself to do) and then it just allows you to use all those classes in a much easier way, really by only creating a message object and sending it through the transaction class.

Basically, it does everything exactly the same as what you will see from the stock source, but it encapsulates all that so third party developers have much easier access to it and can worry about more important things in their app then sifting through source code to find out how to send a stupid MMS message ha. All of the MMS source can be sooooo confusing, especially if you're just starting out like I was. :P
well I will def look at it when adding mms to SmartMessenger and let you know what I think We in the UK don't use MMS at all hardy, costs too much
The Following User Says Thank You to deanwray For This Useful Post: [ Click to Expand ]
 
klinkdawg
Old
(Last edited by klinkdawg; 28th August 2013 at 10:57 PM.)
#8  
Senior Member - OP
Thanks Meter 1,737
Posts: 572
Join Date: Jul 2010

 
DONATE TO ME
Heads up to anyone who is curious, just pushed google voice support for sending to the library. it is a little tricky to figure out the account stuff, so i'll update the readme when I'm done with classes for the day at school.

EDIT: put google voice stuff in the readme, so anyone looking who wants to try should be good to go.
 
Marx2
Old
#9  
Senior Member
Thanks Meter 5
Posts: 326
Join Date: May 2006
Location: Gliwice
Great library. Do you think your library could deal also with network messages? Allowing to send them is probably similar to sending SMS (if not exactly the same), but tricky is intercepting response
 
klinkdawg
Old
#10  
Senior Member - OP
Thanks Meter 1,737
Posts: 572
Join Date: Jul 2010

 
DONATE TO ME
Quote:
Originally Posted by Marx2 View Post
Great library. Do you think your library could deal also with network messages? Allowing to send them is probably similar to sending SMS (if not exactly the same), but tricky is intercepting response
What network messages do you mean? Google voice messages are sent over the internet if that is what you are talking about?

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes