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">

21 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. thanks. it helps a lot. should include

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

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

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

    ReplyDelete
  15. Well written and interesting thoughts. Its amazing how sometimes we get inspiration from the most unexpected of quarters !
    school app in chennai

    ReplyDelete
  16. Hey, Really amazing..!
    I've bookmarked your post for reference and will love to follow you.
    Please keep it up good work for us.
    We provide a solution to use WhatsApp in your business communications and send invoices though tally.
    WhatsApp API Integration
    WhatsApp API Tally
    Tally Invoices to WhatsApp

    ReplyDelete
  17. best induction cooktop. creates unique index on table/view. You cannot create unique index on duplicate columns and

    ReplyDelete
  18. Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agian afghani cap , antique gold choker necklace , embroidered crown patches , Afghani Hand Embroidered Shirt

    ReplyDelete
  19. This is so fun! What a great content. Also I love how authentic you seem to be.

    ReplyDelete