title: Android SMS Service that run in background and get instant updated
body: <p>I am new to android platform and I wish to made an application in which there are no of end devices and a coordinator and they will communicate via sms.</p>
  
  <p>I have built it in j2me but now in android I want to make a service that receive sms in background and update the database uninterruptedly. I have written some code that works fine in simple class but in service how can I bind it></p>
  
  <pre><code>public class SMSReceiver extends BroadcastReceiver {
      private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
      private SMSSync sync;
      private Context context;
      public SMSReceiver(Context ctx){
          sync = new SMSSync(ctx);
          this.context = ctx;
      }
  
      @Override
      public void onReceive(Context context,final Intent intents){
          if (intents.getAction().equals(SMS_RECEIVED)) {
              new Thread(){
                  Context context;
                  Thread Set(Context ctx){
                      this.context=ctx;
                      return this;
              }
          public void run(){
              try{
              SmsManager sms = SmsManager.getDefault();
              Bundle bundle = intents.getExtras();
              /*String uriContent =intent.getDataString();
              String[] str = uriContent.split(":");
              String strPort = str[str.length-1];
              short Port =  Short.valueOf(strPort, 16);*/
              //if(Port==5000){*/
              if (bundle != null) {
              Object[] pdus = (Object[]) bundle.get("pdus");
              SmsMessage[] messages = new SmsMessage[pdus.length];
              for (int i = 0; i &lt; pdus.length; i++)
              messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
              String msg=null;
              String address = null;
                  for (SmsMessage message : messages) {
                       msg = message.getMessageBody();
                       address = message.getOriginatingAddress();
                  }
                  ////////////sending to SMSSync class//////////////
                  MainThread th = new MainThread(sync,msg);
                  try{
                      th.thread.join();
                  }catch(Exception e){
                      Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                  }
                  if(msg.substring(3, 4).equals("2"))
                      ConstantClass.isAuditrequestSend = true;
  //              sync.smsProcess(msg);
                  //Toast.makeText(context, "Message Received Successfully", Toast.LENGTH_SHORT).show();              
              }
  //          }
              ////////////////after receiving the sms opening the Main Screen./////////////////////
              Intent intent = new Intent(context,ZigbeeActivity.class);
              intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(intent);
  
  
          }catch(Exception e){
              Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
          }
          }
      }.Set(context).start();
      }
      }
  //}
  }
  </code></pre>