Receiving SMS BroadcastReceiver Intent Android

In this blog post, you can learn how to receive an sms using a Broadcast Receiver with Intent in android programming. By learning this basic sample you can develop complex android based sms applications.

1. Create a broadcast receiver class for receiving the sms.

Receiver.java

package com.udhaya.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 = "SMS 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();
 }
}

2. Configure and use the below android manifest file. Give Uses permission as highlighted below.

AndroidManifest.xml

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

No comments:

Post a Comment

My Profile