title: How to call broadcast receiver only once in android?
body: <p>Hello I faced the same problem I have 1 broadcast receiver from that I started 1 service which is for getting messages from inbox. After that from that service I send those messages to the particular number. And for the sending message I used the same code as above(posted by John). My problem is, when I send message the toast Message "SMS SENT" will come continuously. So how can I receive that only once?</p>
  
  <p>I am stuck on this please can anybody suggest me where I am wrong?</p>
  
  <p>Thanks.</p>
  
  <p>Here I posted my code:-</p>
  
  <pre><code>package z.z.z;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.telephony.SmsMessage;
  import android.util.Log;
  
  public class SMSReceiver extends BroadcastReceiver  
  {
      private SmsMessage[] msgs;
      private String strNo,strMsgText;
  
      @Override
      public void onReceive(Context context, Intent intent) 
      {
          try
          {
              Bundle bundle = intent.getExtras();        
              msgs = null;
              String str = "";         
  
              if (bundle != null)
              {
                  Object[] pdus = (Object[]) bundle.get("pdus");
                  msgs = new SmsMessage[pdus.length];
                  for (int i=0; i&lt;msgs.length; i++)
                  {
                      msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                      str += "SMS from " + msgs[i].getOriginatingAddress();                     
                      str += ":";
                      str += msgs[i].getMessageBody().toString();
                      str += "\n";
                      strNo = msgs[i].getOriginatingAddress();    
                      strMsgText = msgs[i].getDisplayMessageBody();
  
                  }
  
                  context.startService(new Intent(context,IncomingSMSService.class));
  
              }
          }
          catch (Exception e) 
          {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>IncomingSMSService:-</p>
  
  <pre><code>package z.z.z;
  
  import java.util.ArrayList;
  
  import z.z.z.Global;
  import z.z.z.SMSService;
  
  import android.app.Service;
  import android.content.Intent;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.IBinder;
  import android.util.Log;
  
  public class IncomingSMSService extends Service  
  {
  
      private String TAG = "IncomingSMSService";
  
      @Override
      public IBinder onBind(Intent intent)
      {
          return null;
      }
  
      @Override
      public void onCreate() 
      {
          /* start service */
  
          Log.d(TAG ,"****** in service onCreate  **----- ");
          super.onCreate();
  
          try
          {
              Log.d(TAG ,"****** in service try  **----- ");
              /* Read SMS from INBOX  */
              Uri uriSMSURI = Uri.parse("content://sms/inbox");      
              Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);      
              String sms = "";      
              while (cur.moveToNext()) 
              {          
                      sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
      //              Log.d(TAG, "in SMS Service sms in loop :: "+ sms);
              }
              Log.d(TAG, "in SMS Service sms :: "+ sms);
  
  
              Log.d(TAG ,"********** reverseNum ******  "+ Global.destNo);
              ArrayList&lt;String&gt; ayyMsg = new ArrayList&lt;String&gt;();
              ayyMsg.add(sms);
              SMSService.sendSMS(getBaseContext(), ayyMsg, Global.destNo);
          }
          catch (Exception e) 
          {
              e.printStackTrace();
          }
      }}
  </code></pre>
  
  <p>SendSMS</p>
  
  <pre><code>   package z.z.z;
  
      import java.util.ArrayList;
  
      import z.z.z.common.RMAReceiver;
  
      import android.app.PendingIntent;
      import android.content.Context;
      import android.content.Intent;
      import android.content.IntentFilter;
      import android.telephony.SmsManager;
      import android.util.Log;
  
      public class SMSService 
      {
  
          static String TAG = "SMSService";
          static String SENT = "SMS_SENT";
          static String DELIVERED = "SMS_DELIVERED";
  
      //  static Context mContext;
          static RMAReceiver rmaReceiver = null;
          public static void sendSMS(Context context,ArrayList&lt;String&gt; message, String destNumber)
          {   
              try
              {
  
  
  
                  PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,new Intent(SENT), 0);
  
                  PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,new Intent(DELIVERED), PendingIntent.FLAG_CANCEL_CURRENT);
  
                  ArrayList&lt;PendingIntent&gt; ayySentPI = new ArrayList&lt;PendingIntent&gt;();
                  ayySentPI.add(sentPI);
  
                  ArrayList&lt;PendingIntent&gt; ayyDeliveredPI = new ArrayList&lt;PendingIntent&gt;();
                  ayyDeliveredPI.add(deliveredPI);
  
                  receiver(context);
  
                  SmsManager sms = SmsManager.getDefault();
  
                  sms.sendMultipartTextMessage(destNumber, null, message, ayySentPI, ayyDeliveredPI);
  
              }
              catch (Exception e) 
              {
                  e.printStackTrace();
              }
          }
  
  **receiver**:-
  
      public static void receiver(Context context)
          {
              rmaReceiver = RMAReceiver.getSingleInstance();
              context.registerReceiver(rmaReceiver, new IntentFilter(SENT));
  
              //---when the SMS has been delivered---
              context.registerReceiver(rmaReceiver, new IntentFilter(DELIVERED));  
          }
  </code></pre>
  
  <p>RMAReceiver</p>
  
  <pre><code>package z.z.z;
  
  import z.z.z.SMSService;
  
  import android.app.Activity;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.telephony.SmsManager;
  import android.widget.Toast;
  
  public class RMAReceiver extends BroadcastReceiver 
  {
      private static RMAReceiver singleInstance = null;
      String msg;
  
      private RMAReceiver()
      {       
      }
  
      public static RMAReceiver getSingleInstance()
      {
          if(singleInstance == null) singleInstance = new RMAReceiver();
          return singleInstance;
      }
  
      @Override
      public void onReceive(Context context, Intent intent) 
      {
          System.out.println("Result Code: "+getResultCode());
  //      resultCode = getResultCode();
          switch (getResultCode())
          {
              case Activity.RESULT_OK:
                  msg = "SMS sent/delivered";
  //              Toast.makeText(context, "SMS sent/delivered", 
  //                      Toast.LENGTH_SHORT).show();
  //              SMSService.sendSMS(context, "SMS sent/delivered", Global.confirmNo);
                  break;
              case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                  msg = "Generic failure";
  //              Toast.makeText(context, "Generic failure", 
  //                      Toast.LENGTH_SHORT).show();
  //              SMSService.sendSMS(context, "Generic failure", Global.confirmNo);
                  break;
              case SmsManager.RESULT_ERROR_NO_SERVICE:
                  msg = "No service";
  //              Toast.makeText(context, "No service", 
  //                      Toast.LENGTH_SHORT).show();
  //              SMSService.sendSMS(context, "No service", Global.confirmNo);
                  break;
              case SmsManager.RESULT_ERROR_NULL_PDU:
                  msg = "Null PDU";
  //              Toast.makeText(context, "Null PDU", 
  //                      Toast.LENGTH_SHORT).show();
  //              SMSService.sendSMS(context, "No service", Global.confirmNo);
                  break;
              case SmsManager.RESULT_ERROR_RADIO_OFF:
                  msg = "Radio off";
  //              Toast.makeText(context, "Radio off", 
  //                      Toast.LENGTH_SHORT).show();
  //              SMSService.sendSMS(context, "No service", Global.confirmNo);
                  break;
              case Activity.RESULT_CANCELED:
                  msg = "SMS not delivered";
  //              Toast.makeText(context, "SMS not delivered", 
  //                      Toast.LENGTH_SHORT).show();
  //              SMSService.sendSMS(context, "No service", Global.confirmNo);
                  break;
          }
  
          Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
          SMSService.sendSMS(context, msg, Global.confirmNo);
  
  
  
      }
  
  
  }
  </code></pre>