Receiving SMS Using BroadcastReceiver and Intent

The following sample program is describe how to Receive sms using broadcast receiver with Intent
Step 1
Create broadcast receiver class for receiving sms

package com.javaorigin.android.sample;

import android.content.*;
import android.os.Bundle;
import android.telephony.*;
import android.util.Log;
import android.widget.Toast;

public class SimpleSmsReciever extends BroadcastReceiver {

private static final String TAG = "Message recieved";

@Override
public void onReceive(Context context, Intent intent) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i(TAG, messages.getMessageBody());
Toast.makeText(context, "SMS Received : "+messages.getMessageBody(),
Toast.LENGTH_LONG).show();
}

}


Step 2 :
Configure the AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest android="http://schemas.android.com/apk/res/android"
package="com.javaorigin.android.sample" versioncode="1" versionname="1.0">
<application icon="@drawable/icon" label="@string/app_name">
<receiver name=".SimpleSmsReciever">
<intent-filter>
<action name="android.provider.Telephony.SMS_RECEIVED">
</action>
</intent-filter>
</receiver>
</application>
<uses-sdk minsdkversion="6">
<uses-permission name="android.permission.INTERNET">

16 comments :

  1. Hi,

    Question...
    Let's say I have an activity wich should draw something depending on the received message....

    vDraw is my view to draw in (ViewDraw.java)
    SMSDraw is my main activity (SMSDraw.java)
    the drawing part is working...
    Now how can I get the information from the sms in my SMSdraw ????

    ReplyDelete
  2. wether we have to register the recever ? in above exaample

    ReplyDelete
  3. Is it worked on emulator> i m trying on emulator but not not getting toast nor printing log...

    ReplyDelete
  4. It works but y to add internet permission???

    ReplyDelete
  5. require "android.permission.RECEIVE_SMS" Permission in manifest file to work.

    ReplyDelete
  6. i can't get the code running it always force closes when i run the application.what could be the reason.I've tried getting it to work but still unsuccessful

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. after receives sms, protect on the native inbox(doesn't go on inbox) Pls help me... pls tell the Code... My project pls

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. if i want to read sent messages by me at that time broadcast receiver doesn't working...

    ReplyDelete
  11. Nice Thread Tutorial, i am new in android , its help me a lot ...

    I have got some good links

    here
    at androidexample.com
    Incomming SMS Broadcast Receiver

    ReplyDelete
  12. thanks. it helps a lot. should include

    ReplyDelete
  13. thanks.
    its easy and help a lot.
    i also refer some tutorial at androprogrammer.com

    ReplyDelete
  14. Wow, you are a life saver man. Thanks for the awesome code :D Valentines Day 2014

    ReplyDelete
  15. where is layout.xml file????????

    ReplyDelete