iShader iShader - 1 year ago 135
Android Question

Android – Listen For Incoming SMS Messages

I am trying to create an application for monitoring incoming SMS messages, and launch a program via incoming SMS, also it should read the content from the SMS.


SMS sent to Android device
self executable Application
Read the SMS information

If any one can help me!

Answer Source
public class SmsListener extends BroadcastReceiver{

    private SharedPreferences preferences;

    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                }catch(Exception e){
//                            Log.d("Exception caught",e.getMessage());

Note: In your manifest file add the BroadcastReceiver-

<receiver android:name=".listener.SmsListener">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />

Add this permission:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download