title: Starting a background service for only once and stop once the task is completed
body: <p>I am using a auto-complete text view in my activity and i wrote a service to get data from server in the same activity after completing service i have enabled that auto-complete text view because when user start typing in that text view the data i got from server start gets filtering, every thing up to here is working fine but every time i came to same activity my service start executing i just want that my service get call for only once and also the user can search from auto complete text view once my data come from server.</p>
  
  <p><strong>MyActivity Code</strong></p>
  
  <pre><code>@Override
   protected void onStart() {
  
  
  
      if(CarproApp.getInstance().namesList != null) {
      //enable textfield if you have already fetched the data from server :)
          debitornameET.setEnabled(true);
      }else{
          myReceiver = new MyReceiver();
          IntentFilter intentFilter = new IntentFilter();
          intentFilter.addAction(MyService.MY_ACTION);
          registerReceiver(myReceiver, intentFilter);
          //Start our own service
  
      }
  
      Intent intent = new Intent(CustomerAccountActivity.this,
              MyService.class);
      //intent.putExtra("INIT_DATA", "Data passed from Activity to Service in startService");
      startService(intent);
      super.onStart();
  }
  
  @Override
  protected void onStop() {
      unregisterReceiver(myReceiver);
      super.onStop();
  }
  
  private class MyReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context arg0, Intent arg1) {
          namelist = arg1.getStringArrayListExtra("listnames");
          System.out.println("PRINTING" + namelist);
          debitornameET.setEnabled(true);
          Toast.makeText(getApplicationContext(), "BackGround Task   Completed", Toast.LENGTH_SHORT).show();
          CarproApp.getInstance().namesList = namelist;
  
      }
  
     debitornameET.addTextChangedListener(new TextWatcher() {
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
  
              runOnUiThread(new Runnable() {
                  public void run() {
  
                      //  MyWebRequestService.listVal; 
                      debitorcodeET.setText("");
                      debitorCode = "";
                      Collections.sort(namelist);//here i am getting null when i came back to same activity
                      ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(getApplicationContext(), android.R.layout.simple_dropdown_item_1line,namelist);
                      dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                      debitornameET.setThreshold(1);
                      debitornameET.setAdapter(dataAdapter);
  
  
                  }
              });
          }
  </code></pre>
  
  <p><strong>Service.Class</strong></p>
  
  <pre><code> public class MyService extends Service {
  
      final public static String MY_ACTION = "MY_ACTION";
      //String initData;
      private SendHttpRequest reqService;
      public static ArrayList&lt;String&gt; servicelist;  
  
      @Override
      public IBinder onBind(Intent arg0) {
  
          return null;
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          servicelist = new ArrayList&lt;String&gt;();
      //  initData = intent.getStringExtra("INIT_DATA");
  
          new AsynchCall().execute();
  
          return super.onStartCommand(intent, flags, startId);
      }
  
  
      class AsynchCall extends AsyncTask&lt;String,String,String&gt;{
  
          @Override
          protected String doInBackground(String... params) {
              String qryService = "SELECT DEBITOR_NAME FROM DEBITORS ";
              reqService = new SendHttpRequest(qryService);
              try {
                  reqService.ExecuteQuery();
              } catch (URISyntaxException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (ParserConfigurationException e) {
                  e.printStackTrace();
              } catch (SAXException e) {
  
                  e.printStackTrace();
              }
  
              for(int i=0;i&lt;reqService.getRowCount();i++)
              {
                  servicelist.add(reqService.getRow(i)[1]);
                  System.out.println("TEXTVIEWDATAAAAAAAAa" + reqService.getRow(i)[1]);
              }
              Intent intent = new Intent();
              intent.setAction(MY_ACTION);
              intent.putStringArrayListExtra("listnames", servicelist);
              sendBroadcast(intent);
              return null;
          }
  
      }
  }
  </code></pre>
  
  <p>** CarproApp Class**</p>
  
  <pre><code>        public class CarproApp extends Application {
  
          private static CarproApp sInstance;
          public ArrayList&lt;String&gt; namesList;
  
          public static CarproApp getInstance() {
              return sInstance;
          }
  
          public void onCreate() {
              super.onCreate();
              sInstance = this;
  
  
          }
  
  
  
      }
  </code></pre>
  
  <p>Because the data coming from server is so large i don't want it to store in local database. My code is working but every time i came to activity the service call gets executed which i don't want. Please guide me </p>