Android: Recieve SMS(How to recieve an sms in Android)

Leave a Comment

How to create an App that notifies you when you recieve an SMS.

This code notifies you when an sms is recieved an then shown as a notification  using a Toast.
Create a project called SMSnotify. if you have any problem creating the project refer to these steps here

After creating the project create a class called SMSNotification.java and paste the following code.

 SMSNotification.java

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class
SMSNotification extends BroadcastReceiver {
   
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private static final String TAG = "SMSBroadcastReceiver";
    Context cont;

    @SuppressLint("NewApi")
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i(TAG, "Intent recieved: " + intent.getAction());
        cont = context;
        if (intent.getAction().equals(SMS_RECEIVED)) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                final SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }
                if (messages.length > -1) {

                    Toast.makeText(
                            context,
                            "Message recieved: " + messages[0].getMessageBody()
                                    + messages[0].getOriginatingAddress(), 7000)
                            .show();
                       
                    }
                    }
                    }
                    } 


On the Manifest file add the following highlighted code below. Add the rquired permissions as show below also.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="
com.example.smsnotify"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
 <uses-permission android:name="android.permission.RECEIVE_SMS"/>
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 <uses-permission android:name="android.permission.SEND_SMS"/>

    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.
smsnotify.MainActivity"
            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=".
SMSNotification" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>
        </receiver>

    </application>

</manifest>

 
 Run your application and try sending an SMS to your phone. the sms recieved will be show on the application you created as Toast.

Enjoy coding.........

0 comments:

Post a Comment