Wednesday, January 30, 2013

[android-developers] Re: Sending and Receiving SMS/MMS in Android

MMS messages are actually sent and received via HTTP connections to a special server called an MMSC. The WAP push you get when an MMS is received contains the message ID used in the HTTP GET to retrieve the message.

The problem is the MMSC is owned and operated by the cellular network, and they won't allow your app to access it.

Your best bet is to piggyback on the built-in messaging app. Inserting a message into the MMS content provider's Outbox should cause the messaging app to send it (assuming the device has the content provider (which is a pretty good assumption)).

-Erik

On Thursday, January 24, 2013 1:01:22 AM UTC-8, Etienne wrote:

I have figured out how to send and receive SMS messages. To send SMS messages I had to call thesendTextMessage() and sendMultipartTextMessage() methods of the SmsManager class. To receive SMS messages, I had to register a receiver in the AndroidMainfest.xml file. Then I had to override the onReceive() method of the BroadcastReceiver. I have included examples below.

MainActivity.java

public class MainActivity extends Activity {      private static String SENT = "SMS_SENT";      private static String DELIVERED = "SMS_DELIVERED";      private static int MAX_SMS_MESSAGE_LENGTH = 160;        // ---sends an SMS message to another device---      public static void sendSMS(String phoneNumber, String message) {            PendingIntent piSent = PendingIntent.getBroadcast(mContext, 0, new Intent(SENT), 0);          PendingIntent piDelivered = PendingIntent.getBroadcast(mContext, 0,new Intent(DELIVERED), 0);          SmsManager smsManager = SmsManager.getDefault();            int length = message.length();                    if(length > MAX_SMS_MESSAGE_LENGTH) {              ArrayList<String> messagelist = smsManager.divideMessage(message);                        smsManager.sendMultipartTextMessage(phoneNumber, null, messagelist, null, null);          }          else              smsManager.sendTextMessage(phoneNumber, null, message, piSent, piDelivered);          }      }        //More methods of MainActivity ...  }

SMSReceiver.java

public class SMSReceiver extends BroadcastReceiver {      private final String DEBUG_TAG = getClass().getSimpleName().toString();        public void onReceive(Context context, Intent intent) {            // ---get the SMS message passed in---          Bundle bundle = intent.getExtras();          SmsMessage[] msgs = null;          String str = "";          int contactId = -1;          String address;            if (bundle != null) {              // ---retrieve the SMS message received---              Object[] pdus = (Object[]) bundle.get("pdus");              msgs = new SmsMessage[pdus.length];              for (int i = 0; i < msgs.length; i++) {                  msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);                    address = msgs[i].getOriginatingAddress();                  contactId = ContactsUtils.getContactId(mContext, address, "address");                  str += msgs[i].getMessageBody().toString();                  str += "\n";          }               if(contactId != -1){              showNotification(contactId, str);          }            // ---send a broadcast intent to update the SMS received in the          // activity---          Intent broadcastIntent = new Intent();          broadcastIntent.setAction("SMS_RECEIVED_ACTION");          broadcastIntent.putExtra("sms", str);          context.sendBroadcast(broadcastIntent);      }        /**      * The notification is the icon and associated expanded entry in the status      * bar.      */      protected void showNotification(int contactId, String message) {          //Display notification...      }  }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.myexample"      android:versionCode="1"      android:versionName="1.0" >        <uses-sdk          android:minSdkVersion="16"          android:targetSdkVersion="17" />        <uses-permission android:name="android.permission.READ_CONTACTS" />      <uses-permission android:name="android.permission.READ_PHONE_STATE" />      <uses-permission android:name="android.permission.SEND_SMS" />      <uses-permission android:name="android.permission.RECEIVE_SMS" />      <uses-permission android:name="android.permission.READ_SMS" />      <uses-permission android:name="android.permission.WRITE_SMS" />      <uses-permission android:name="android.permission.RECEIVE_MMS" />      <uses-permission android:name="android.permission.WRITE" />      <uses-permission android:name="android.permission.VIBRATE" />      <uses-permission android:name="android.permission.INTERNET" />      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />        <application          android:debuggable="true"          android:icon="@drawable/ic_launcher_icon"          android:label="@string/app_name" >            <activity              //Main activity...              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                    <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>          <activity              //Activity 2 ...          </activity>          //More acitivies ...            // SMS Receiver          <receiver android:name="com.myexample.receivers.SMSReceiver" >              <intent-filter>                  <action android:name="android.provider.Telephony.SMS_RECEIVED" />              </intent-filter>          </receiver>        </application>  </manifest>

However, I was wondering if you could send and receive MMS messages in a similar fashion. After doing some research, many examples provided on blogs simply pass an Intent to the native Messaging application. I am trying to send an MMS without leaving my application. There doesn't seem to be a standard way of sending and receiving MMS. Has anyone gotten this to work?

Also, I am aware that the SMS/MMS ContentProvider is not a part of the official Android SDK, but I was thinking someone may have been able to implement this. Any help is greatly appreciated.

UPDATE

I have added a BroadcastReceiver to the AndroidManifest.xml file to receive MMS messages

<receiver android:name="com.sendit.receivers.MMSReceiver" >      <intent-filter>          <action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />            <data android:mimeType="application/vnd.wap.mms-message" />      </intent-filter>  </receiver>

In the MMSReceiver class, the onReceive() method is only able to grab the phoneNumber that the message was sent from. How do you grab other important things from an MMS such as the file path to the media attachment (image/audio/video), or the text in the MMS?

MMSReceiver.java

public class MMSReceiver extends BroadcastReceiver {      private final String DEBUG_TAG = getClass().getSimpleName().toString();      private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_RECEIVED";      private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";        public void onReceive(Context context, Intent intent) {            //Retrieve MMS---          String action = intent.getAction();          String type = intent.getType();            if(action.equals(ACTION_MMS_RECEIVED) && type.equals(MMS_DATA_TYPE)){                Bundle bundle = intent.getExtras();                Log.d(DEBUG_TAG, "bundle " + bundle);              SmsMessage[] msgs = null;              String str = "";              int contactId = -1;              String address;                if (bundle != null) {                    byte[] buffer = bundle.getByteArray("data");                  Log.d(DEBUG_TAG, "buffer " + buffer);                  String incomingNumber = new String(buffer);                  int indx = incomingNumber.indexOf("/TYPE");                  if(indx>0 && (indx-15)>0){                      int newIndx = indx - 15;                      incomingNumber = incomingNumber.substring(newIndx, indx);                      indx = incomingNumber.indexOf("+");                      if(indx>0){                          incomingNumber = incomingNumber.substring(indx);                          Log.d(DEBUG_TAG, "Mobile Number: " + incomingNumber);                      }                  }                    int transactionId = bundle.getInt("transactionId");                  Log.d(DEBUG_TAG, "transactionId " + transactionId);                    int pduType = bundle.getInt("pduType");                  Log.d(DEBUG_TAG, "pduType " + pduType);                    byte[] buffer2 = bundle.getByteArray("header");                        String header = new String(buffer2);                  Log.d(DEBUG_TAG, "header " + header);                    if(contactId != -1){                      showNotification(contactId, str);                  }                    // ---send a broadcast intent to update the MMS received in the                  // activity---                  Intent broadcastIntent = new Intent();                  broadcastIntent.setAction("MMS_RECEIVED_ACTION");                  broadcastIntent.putExtra("mms", str);                  context.sendBroadcast(broadcastIntent);                }          }        }        /**      * The notification is the icon and associated expanded entry in the status      * bar.      */      protected void showNotification(int contactId, String message) {          //Display notification...      }  }

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment