Monday 25 July 2011

MMS Listener

How to listen for Incoming MMS and Retrieve it's content? Here is the solution:

public class MmsListener extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if(intent.getAction().equals("android.provider.Telephony.WAP_PUSH_RECEIVED")){
            Bundle bundle = intent.getExtras();
            try{
                if (bundle != null){
                    //Retrieve MMS---
                    String type = intent.getType();
                    if(type.trim().equalsIgnoreCase("application/vnd.wap.mms-message")){
                        byte[] buffer = bundle.getByteArray("data");
                        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);
                                System.out.println("Mobile Number: " + incomingNumber);
                            }
                        }
                    }
                }
            }catch(Exception e){
/                       Log.d("MMS data Exception caught", e.getMessage());
            }
        }
              
    }

}


Note: In your manifest file add the BroadcastReceiver -

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

Permission:
<uses-permission android:name="android.permission.RECEIVE_MMS"/>



2 comments:

  1. Is it possible to grab the date, image, text from the intent as well?

    ReplyDelete
  2. Nice, this works to grab the number, but how about the body of the mms (like the text or the image/audio/video)?

    ReplyDelete