-1

I would like a send an intent to this receiver from another application, which handles SMS. I am quite new to this SMS handling. Can someone kindly guide me on what intent,I mean, how and what and intent should have to execute this piece of receiver code. Thanks.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
        try {
            Object[] objArr = (Object[]) extras.get("pdus");
            for (Object obj : objArr) {
                SmsMessage createFromPdu = SmsMessage.createFromPdu((byte[]) obj);
                String displayOriginatingAddress = createFromPdu.getDisplayOriginatingAddress();
                String displayMessageBody = createFromPdu.getDisplayMessageBody();
                try {
                    if (displayOriginatingAddress.contains("MADAPP")) {
                        if (displayMessageBody.contains("The PIN is")) {
                            Toast.makeText(context, displayMessageBody, 1).show();

                        }
                        if (displayMessageBody.contains("successfully validated")) {
                            displayMessageBody.contains("activating Pockets");
                        }
                    }
                } catch (Exception e) {
                }
            }
        } catch (Exception e2) {
        }
    }
}

}

abi pratha
  • 17
  • 3

1 Answers1

0

Try this code

write this in your manifest

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" />
  <receiver android:name=".SMSReceiver">   
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

For more description

http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62&aaid=87

  • Thanks for the reply, But if i want to trigger this piece of code from other application, what should i do? – abi pratha Apr 27 '16 at 10:50
  • you can set your custom action which will be recieved by this broadcast reciever. for ex. ` ` You can go through this link for more details https://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/ – Digital Aptech Apr 27 '16 at 13:32