Android sends MMS messages. The system MMS interface is not called.

Source: Internet
Author: User
Tags http 200 sqlite database

5-digit development-android technology development blog

There is a need recently to not call the system interface to send MMs. The first response may be as follows:
Instead of using startactivity, call a method similar to sending a text message like sending a text message.
Smsmanager = smsmanager. getdefault ();
Smsmanager. sendtextmessage (phonecode, null, text, null, null );
Can it be implemented? The answer is no, because there is no interface for sending MMS on Android. If you want to send MMS, sorry, please call the system MMS app interface, as shown below:

 Intent sendIntent = new Intent(Intent.ACTION_SEND,  Uri.parse("mms://"));    sendIntent.setType("image/jpeg");    String url = "file://sdcard//tmpPhoto.jpg";    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));    startActivity(Intent.createChooser(sendIntent, "MMS:"));

However, this method often cannot meet our needs. Can we send MMS by calling the system interface? After several days of hard work, I finally found a solution.

Step 1: First construct the MMS content you want to send, that is, to build a PDU, you need to use the following classes, these classes are from the android source code MMS application. in the PDU package. You need to set all classes in the PDU package

Copy them to your project and call them as appropriate.
Final sendreq sendrequest = new sendreq (); Final pdubody = new pdubody (); Final pdupart part = new pdupart (); // store the attachment. Each attachment is a part, if multiple attachments are added, add multiple parts to the body. Pdubody. addpart (partpdu); sendrequest. setbody (pdubody); Final pducomposer composer = new pducomposer (CTX, sendrequest); Final byte [] bytestosend = composer. make (); // converts the contents and theme of the MMS into byte arrays and sends them to "http://mmsc.monternet.com" over HTTP ";
Step 2: Send the MMS to the MMS center.
 
Code for building PDU:
        
String subject = "test MMS"; string recipient = "number of received MMS"; // 138 xxxxxxx final sendreq sendrequest = new sendreq (); Final encodedstringvalue [] sub = encodedstringvalue. extract (subject); If (sub! = NULL & sub. length> 0) {sendrequest. setsubject (sub [0]);} final encodedstringvalue [] phonenumbers = encodedstringvalue. Extract (recipient); If (phonenumbers! = NULL & phonenumbers. length> 0) {sendrequest. addto (phonenumbers [0]);} final pdubody = new pdubody (); Final pdupart part = new pdupart (); part. setname ("sample ". getbytes (); Part. setcontenttype ("image/PNG ". getbytes (); string furl = "file: // MNT/sdcard // 1.jpg"; Final pdupart partpdu = new pdupart (); partpdu. setcharset (charactersets. utf_8); // utf_16 partpdu. setname (part. getname (); partpdu. Setcontenttype (part. getcontenttype (); partpdu. setdatauri (URI. parse (furl); pdubody. addpart (partpdu); sendrequest. setbody (pdubody); Final pducomposer composer = new pducomposer (CTX, sendrequest); Final byte [] bytestosend = composer. make (); thread t = new thread (New runnable () {@ overridepublic void run () {try {httpconnectinterface. sendmms (CTX, bytestosend); //} catch (ioexception e) {e. printstacktra Ce () ;}}); T. start (); Code for sending a PDU to the MMS center: public static string mmscurl = "http://mmsc.monternet.com"; // public static string mmscurl = "http://www.baidu.com /"; public static string mmsproxy = "10.0.0.172"; public static string mmsprot = "80"; Private Static string hdr_value_accept_language = ""; // definition for necessary HTTP headers. private Static final string hdr_key_accept = "accept"; Private Static Final string hdr_key_accept_language = "Accept-language"; Private Static final string hdr_value_accept = "*/*, application/vnd. WAP. MMS-message, application/vnd. WAP. sic "; public static byte [] sendmms (context, byte [] PDU) throws ioexception {hdr_value_accept_language = gethttpacceptlanguage (); If (mmscurl = NULL) {Throw new illegalargumentexception ("url must not be null. ");} httpclient Client = NULL; try {// make sure to use a proxy which supports connect. client = httpconnector. buileclient (context); httppost post = new httppost (mmscurl); // MMS pud start bytearrayentity entity = new bytearrayentity (PDU); entity. setcontenttype ("application/vnd. WAP. MMS-message "); Post. setentity (entity); Post. addheader (hdr_key_accept, hdr_value_accept); Post. addheader (hdr_key_accept_language, hdr_value_a Ccept_language); // MMS pud end httpparams Params = client. getparams (); httpprotocolparams. setcontentcharset (Params, "UTF-8"); httpresponse response = client.exe cute (post); logutility. showlog (TAG, "111"); statusline status = response. getstatusline (); logutility. showlog (TAG, "status" + status. getstatuscode (); If (status. getstatuscode ()! = 200) {// HTTP 200 is not success. logutility. showlog (TAG ,"! 200 "); throw new ioexception (" HTTP Error: "+ status. getreasonphrase ();} httpentity resentity = response. getentity (); byte [] body = NULL; If (resentity! = NULL) {try {If (resentity. getcontentlength ()> 0) {body = new byte [(INT) resentity. getcontentlength ()]; datainputstream Dis = new datainputstream (resentity. getcontent (); try {dis. readfully (body);} finally {try {dis. close ();} catch (ioexception e) {log. E (TAG, "error closing input stream:" + E. getmessage () ;}}} finally {If (entity! = NULL) {entity. consumecontent () ;}} logutility. showlog (TAG, "Result:" + new string (body); return body;} catch (illegalstateexception e) {logutility. showlog (TAG, "", e); // handlehttpconnectionexception (E, mmscurl);} catch (illegalargumentexception e) {logutility. showlog (TAG, "", e); // handlehttpconnectionexception (E, mmscurl);} catch (socketexception e) {logutility. showlog (TAG, "", e); // handle Httpconnectionexception (E, mmscurl);} catch (exception e) {logutility. showlog (TAG, "", e); // handlehttpconnectionexception (E, mmscurl);} finally {If (client! = NULL) {// client.;} return New byte [0];}

 

At this point, the sending of MMS is complete.
Summary: There is no API for MMS-related operations on Android, including reading, sending, and storing MMs. These processes must be completed manually. To understand these processes, read the MMS app in the android source code carefully. The other is to study the mmssms. DB database, because the read and storage of MMS is actually the operation process of the mmssms. DB database. Because this is a shared database, you can only use the contentprovider component to operate the database.

In short, to study MMS (including common text messages), you must study mmssms. the database operation method provides a better understanding of the URI corresponding to each table, the operations that each URI can provide, and the fields that represent the attributes of the text message.
Finally, we recommend a good SQLite viewing tool: SQLite Database Browser.

5-digit development-android technology development blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.