Receiving SMS Using BroadcastReceiver and Intent

Receiving SMS Using BroadcastReceiver and Intent

The following sample program describes how to Receive SMS using broadcast receiver with Intent

Step 1
Create broadcast receiver class for receiving SMS.

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

 

Leave a Reply

Your email address will not be published. Required fields are marked *