SoFunction
Updated on 2025-04-09

Android programming method (MMS) is used to implement the method of sending multimedia messages in non-call system interface

This article describes the method of sending MMS messages through the Android non-call system interface. Share it for your reference, as follows:

1. Question:

Recently, there is a requirement that you do not call the system interface to send MMS function. Students who have done SMS function may have the first reaction:

Without StartActivity, call a method similar to sending a text message like sending a text message

SmsManager smsManager = ();
(phoneCode, null, text, null, null);

2. Solution:

Since there is no interface for sending MMS on Android, if you want to send MMS, I'm sorry, please call the system MMS app interface, as follows:

Intent sendIntent = new Intent(Intent.ACTION_SEND, ("mms://"));
("image/jpeg");
String url = "file://sdcard//";
(Intent.EXTRA_STREAM, (url));
startActivity((sendIntent, "MMS:"));

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

The first step: first construct the MMS content you want to send, that is, to build a pdu, the following classes are required, which are copied from the MMS application package of Android source code. You need to add all the classes in the pdu package

Copy them all into your project and then adjust them at your own discretion.

final SendReq sendRequest = new SendReq();
final PduBody pduBody = new PduBody();
final PduPart part = new PduPart();
//Storage attachments, each attachment is a part. If you add multiple attachments, you want to add multiple parts in the body.(partPdu);
(pduBody);
final PduComposer composer = new PduComposer(ctx, sendRequest);
final byte[] bytesToSend = ();
//Convert the content and theme of the MMS into a byte array, and prepare to pass the http protocol//Send to ””;

Step 2: Send MMS to MMS Center.

Code to build pdu:

String subject = "Test MMS";
String recipient = "Number of receiving MMS";//138xxxxxxx
final SendReq sendRequest = new SendReq();
final EncodedStringValue[] sub = (subject);
if (sub != null &&  > 0) {
(sub[0]);
}
final EncodedStringValue[] phoneNumbers = (recipient);
if (phoneNumbers != null &&  > 0) {
(phoneNumbers[0]);
}
final PduBody pduBody = new PduBody();
final PduPart part = new PduPart();
("sample".getBytes());
("image/png".getBytes());
String furl = "file://mnt/sdcard//";
final PduPart partPdu = new PduPart();
(CharacterSets.UTF_8);//UTF_16
(());
(());
((furl));
(partPdu);
(pduBody);
final PduComposer composer = new PduComposer(ctx, sendRequest);
final byte[] bytesToSend = ();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
  try {
    (ctx, bytesToSend);
  //
  } catch (IOException e) {
    ();
  }
 }
});
();

Code to send pdu to MMS center:

public static String mmscUrl = "";
// public static String mmscUrl = "/";
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/-message, application/";
public static byte[] sendMMS(Context 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 = (context);
  HttpPost post = new HttpPost(mmscUrl);
  //mms PUD START
  ByteArrayEntity entity = new ByteArrayEntity(pdu);
  ("application/-message");
  (entity);
  (HDR_KEY_ACCEPT, HDR_VALUE_ACCEPT);
  (HDR_KEY_ACCEPT_LANGUAGE, HDR_VALUE_ACCEPT_LANGUAGE);
  //mms PUD END
  HttpParams params = ();
  (params, "UTF-8");
  HttpResponse response = (post);
  (tag, "111");
  StatusLine status = ();
  (tag, "status "+());
  if (() != 200) { // HTTP 200 is not success.
    (tag, "!200");
    throw new IOException("HTTP error: " + ());
  }
  HttpEntity resentity = ();
  byte[] body = null;
  if (resentity != null) {
    try {
      if (() > 0) {
        body = new byte[(int) ()];
        DataInputStream dis = new DataInputStream(());
        try {
          (body);
        } finally {
          try {
            ();
          } catch (IOException e) {
            (tag, "Error closing input stream: " + ());
          }
        }
      }
    } finally {
      if (entity != null) {
        ();
      }
    }
  }
  (tag, "result:"+new String(body));
  return body;
 } catch (IllegalStateException e) {
  (tag, "",e);
 //      handleHttpConnectionException(e, mmscUrl);
 } catch (IllegalArgumentException e) {
  (tag, "",e);
 //      handleHttpConnectionException(e, mmscUrl);
 } catch (SocketException e) {
  (tag, "",e);
 //      handleHttpConnectionException(e, mmscUrl);
 } catch (Exception e) {
  (tag, "",e);
  //handleHttpConnectionException(e, mmscUrl);
 } finally {
  if (client != null) {
 //        client.;
  }
 }
 return new byte[0];
}

At this point, the sending of MMS has been completed.

Summary: Android's MMS related operations do not have APIs, including reading, sending and storing of MMS. These processes need to be done manually. To understand these processes, you need to carefully read the mms app in the Android source code. Also, we need to study the database, because the reading and storage of MMS are actually the operation process of this database. And because this is a shared database, you can only use the ContentProvider component to operate db.

In short, if you want to study MMS (including ordinary SMS), you must study the operation methods, understand more about which uri corresponds to each table, what kind of operations each uri can provide, and the properties of those fields representing the SMS.

Finally, I recommend a useful SQLite viewing tool: SQLite Database Browser.

I hope this article will be helpful to everyone's Android programming design.