Android programming non call system interface to send MMS method of MMS

  • 2020-12-18 01:54:45
  • OfStack

An example of Android non - call system interface to send MMS method. To share for your reference, the details are as follows:

1. The question is:

Recently, there is a need not to call the system interface to send MMS function. For those of you who have done SMS, your first reaction might be:

Instead of using StartActivity, as with texting, call a method similar to texting


SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneCode, null, text, null, null);

2. Solutions:

Since there is no interface to send MMS at all on android, 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 own implementation of sending MMS? After several days of hard work, a solution was finally found.

Step 1: Build an pdu using the following classes, which are derived from the ES24en.pdu package in the MMS application source code of android. You need to combine all the classes in the pdu package

Copy it all into your project and adjust it as you see fit.


final SendReq sendRequest = new SendReq();
final PduBody pduBody = new PduBody();
final PduPart part = new PduPart();
// Store attachments. Each attachment is 1 a part If you add more than one attachment, just think body In the add multiple part . 
pduBody.addPart(partPdu);
sendRequest.setBody(pduBody);
final PduComposer composer = new PduComposer(ctx, sendRequest);
final byte[] bytesToSend = composer.make();
// Convert the content and subject of the MMS message into byte Array, ready to pass http agreement 
// Sent to the   " http://mmsc.monternet.com " ;

Step 2: Send MMS to the MMS center.

Code to build pdu:


String subject = " Test the MMS ";
String recipient = " The number to receive MMS messages ";//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 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() {
@Override
public void run() {
  try {
    HttpConnectInterface.sendMMS(ctx, bytesToSend);
  //
  } catch (IOException e) {
    e.printStackTrace();
  }
 }
});
t.start();

Code to send pdu to MMS:


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 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_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() != 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);
 //      handleHttpConnectionException(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.

Conclusion: android MMS related operations are not api, including MMS reading, sending, storage. These processes need to be done manually. To understand these processes, read mms, the app, in the android source code carefully. Another thing is to study mmssms.db database, because the reading and storage of MMS are actually the operation process of ES52en.db database. And because this is a shared database, you can only use the component ContentProvider to work with db.

In conclusion, to study MMS (including ordinary SMS messages), you must study mmssms.db's operation method, learn more about which uri is corresponding to each table, what operation each uri can provide, which fields represent the attributes of SMS, etc.

Finally recommended sqlite view to use tools: SQLite Database Browser.

I hope this article has been helpful in Android programming.


Related articles: