Android Programming's non-call system interface to implement the method of sending MMS (MMS) _android

Source: Internet
Author: User
Tags sqlite sqlite database

This article describes the Android non-call system interface to implement the method of sending MMS. Share to everyone for your reference, specific as follows:

First, the question:

Recently there is a demand, not to call the system interface to send MMS features. Students who have done the texting function may first react like this:

Not using startactivity, like texting, calling a method similar to texting

Smsmanager Smsmanager = Smsmanager.getdefault ();
Smsmanager.sendtextmessage (Phonecode, NULL, text, NULL, NULL);

Second, the solution:

Because Android does not provide the interface to send MMS, if you want to send MMS, sorry, please call the System MMS app interface, as follows:

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:");

But this method often can not meet our needs, can not call the system interface, their implementation to send MMS? After several days of efforts, finally found a solution.

First step: Construct the MMS you want to send, that is, to build a PDU, you need to use the following classes, these classes are from the Android source of MMS applications in the MMS.PDU package copy out. You need to have all the classes in the PDU package

are copied to your project and are then transferred to your own discretion.

Final Sendreq sendrequest = new Sendreq ();
Final Pdubody pdubody = new Pdubody ();
Final Pdupart part = new Pdupart ();
Storage attachment, each attachment is a part, if you add more than one attachment, you want to add multiple parts in the body.
Pdubody.addpart (PARTPDU);
Sendrequest.setbody (pdubody);
Final Pducomposer composer = new Pducomposer (CTX, sendrequest);
Final byte[] Bytestosend = Composer.make ();
Convert the contents of MMS and subject information into byte array, prepare to pass HTTP protocol
//Send To "http://mmsc.monternet.com";

Step two: send MMS to MMS Center.

To build the PDU code:

String subject = "Test MMS";
String recipient = "Receive MMS number";//138xxxxxxx 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 PDub
Ody = 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 () {@Override public void run () {try {Httpconnectinterface.sendmms (CTX), Bytesto
  Send);
  catch (IOException e) {e.printstacktrace ();
}
 }
});

 T.start ();

Code to send 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 context, byte[] PDU) throws ioexception{Hdr_value_accept_language =
Gethttpacceptlanguage ();
if (Mmscurl = = null) {throw new IllegalArgumentException ("URL must not is 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_accept_language);
  MMS PUD End Httpparams params = Client.getparams ();
  Httpprotocolparams.setcontentcharset (params, "UTF-8");
  HttpResponse response = Client.execute (POST);
  Logutility.showlog (Tag, "111");
  Statusline status = Response.getstatusline ();
  Logutility.showlog (tag, "status" +status.getstatuscode ());
    if (Status.getstatuscode ()!=) {//HTTP 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) resentit
        Y.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 to 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);
 Handlehttpconnectionexception (E, mmscurl);
  catch (Exception e) {logutility.showlog (tag, "", e);
 Handlehttpconnectionexception (E, mmscurl);
  Finally {if (client!= null) {//client.;
} return to new byte[0];

 }

At this point, the sending of MMS is completed.

Summary: Android MMS-related operations are not API, including MMS reading, sending, storage. All of these processes need to be done manually. To understand these processes, you need to carefully read the MMS app in the Android source. There is to study the MMSSMS.DB database, because the MMS reading and storage is actually mmssms.db this database operation process. And because this is a shared database, you can only use the ContentProvider component to manipulate DB.

In short, want to study MMS this piece (including ordinary text messages), you have to study the mmssms.db operation method, a lot of understanding of each table corresponding to which URI, each URI can provide what kind of operation, those fields represent the attributes of text messages.

Finally recommend a useful SQLite view tool: SQLite Database Browser.

I hope this article will help you with the Android program.

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.