Incomming SMS Broadcast Receiver - Android Example


Simulator Screenshots

Android Example Screenshot 1

Android Example Screenshot 2

Download Code

Login To Download Source Code
    
 
Related Examples
Check_Current_Running_Activity_|_Task_|_Application_-_Android_Example  Check Current Running Activity | Task | Application - Android Example
Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example  Screen Wake Sleep Event Listner Service - Android Example
Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example  Incomming Phone Call Broadcast Receiver - Android Example
Introduction_To_Broadcast_Receiver_Basics  Introduction To Broadcast Receiver Basics

In this android example we are showing ,when any new SMS event brodcasted ( When new SMS received ) then how to create receiver to read SMS data.

NOTE :  If you are new in android developement then first see this article Introduction To Broadcast Receiver Basics

 

Project Structure :

 

custom checkbox project sketch

 

AndroidManifest.xml File Details :

   1. Declare receiver in AndroidManifest

 


       <receiver android:name=".IncomingSms">   
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

 

      2. Give read SMS permission in AndroidManifest

 


     <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
     <uses-permission android:name="android.permission.READ_SMS" />
     <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

 

Complete code for AndroidManifest.xml File :

 


    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.androidexample.broadcastreceiver"
        android:versionCode="1"
        android:versionName="1.0" >
           
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.androidexample.broadcastreceiver.BroadcastNewSms"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            
            <receiver android:name="com.androidexample.broadcastreceiver.IncomingSms">   
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
            
        </application>
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
        
        <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
        
    </manifest>

---------------------------------------------------------------------------------------------------------------------------------------

IncomingSms.java file details :

    I have made broadcast event reciever in this file

     1. Created class IncomingSms with extends BroadcastReceiver class

 


   public class IncomingSms extends BroadcastReceiver

 

      2. Get the object of SmsManager to find out received sms details

 


        // Get the object of SmsManager
        final SmsManager sms = SmsManager.getDefault();

 

      3.  Create method receiver()

 


  public void onReceive(Context context, Intent intent)

 

        4. Get / Read current Incomming SMS data

 


        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {
            
            if (bundle != null) {
                
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                
                for (int i = 0; i < pdusObj.length; i++) {
                    
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
                    

                   // Show alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();
                    
                } // end for loop
              } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);
            
        }

=================================================================================================

     Complete code for  IncomingSms.java file :

 




    public class IncomingSms extends BroadcastReceiver {
        
        // Get the object of SmsManager
        final SmsManager sms = SmsManager.getDefault();
        
        public void onReceive(Context context, Intent intent) {
        
            // Retrieves a map of extended data from the intent.
            final Bundle bundle = intent.getExtras();
    
            try {
                
                if (bundle != null) {
                    
                    final Object[] pdusObj = (Object[]) bundle.get("pdus");
                    
                    for (int i = 0; i < pdusObj.length; i++) {
                        
                        SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                        String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                        
                        String senderNum = phoneNumber;
                        String message = currentMessage.getDisplayMessageBody();
    
                        Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
                        
    
                       // Show Alert
                        int duration = Toast.LENGTH_LONG;
                        Toast toast = Toast.makeText(context, 
                                     "senderNum: "+ senderNum + ", message: " + message, duration);
                        toast.show();
                        
                    } // end for loop
                  } // bundle is null
    
            } catch (Exception e) {
                Log.e("SmsReceiver", "Exception smsReceiver" +e);
                
            }
        }    
    }


 

--------------------------------------------------------------------------------------------------------------------------------------------

 

How to test in simulator :

               Please see below video to test this example in simulator

 


    
 
videos
Google