title: Running an ASync task in a service
body: <p>I am building an app that will check data from a URL in json and display it in the UI. I have created a service that I want to run the Async task. The service works fine and runs fine, I just can't get the task to run. Any ideas?</p>
  
  <pre><code>public class Getmonitors extends AsyncTask&lt;Void, Void, Void&gt; {
  
      // Hashmap for ListView
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; monitorList;
      ProgressDialog pDialog;
  
      @Override
      public void onPreExecute() {
          super.onPreExecute();
          // Showing progress dialog
          pDialog = new ProgressDialog(myActivity.this);
          pDialog.setMessage("Please wait...");
          Log.v(LOG_TAG, "PLEASE WAIT");
          pDialog.setCancelable(false);
          pDialog.show();
      }
  
      @Override
      public Void doInBackground(Void... arg0) {
          // Creating service handler class instance
          NewWebRequest webreq = new NewWebRequest();
  
          // Making a request to url and getting response
          String jsonStr = webreq.makeWebServiceCall(url, NewWebRequest.GET);
  
          monitorList = ParseJSON(jsonStr);
  
          return null;
      }
  
      @Override
      public void onPostExecute(Void result) {
          super.onPostExecute(result);
          // Dismiss the progress dialog
          if (pDialog.isShowing())
              pDialog.dismiss();
          /**
           * Updating parsed JSON data into ListView
           * */
  
       //  Intent intent = new Intent(MainActivity.this, myService.class);
       //            startService(intent);
  
  
          ListAdapter adapter = new SimpleAdapter(
  
                  myActivity.this, monitorList,
                  R.layout.list_item, new String[]{TAG_NAME, TAG_EMAIL,
                  TAG_STATUS}, new int[]{R.id.name,
                  R.id.email, R.id.statuslist});
  
          setListAdapter(adapter);
  
  
       //            setContentView(R.layout.activity_main);
  
          Button button = (Button) findViewById(R.id.button1);
  
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  ImageView iv = (ImageView) findViewById(R.id.imageview1);
                  iv.setVisibility(View.VISIBLE);
  
                  new Getmonitors().execute();
                  iv.setVisibility(View.INVISIBLE);
              }
          });
  
  
          //service onDestroy callback method will be called
          findViewById(R.id.start_service).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(myActivity.this, myService.class);
                  startService(intent);
              }
          });
  
  
          //service onDestroy callback method will be called
          findViewById(R.id.stop_Service).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(myActivity.this, myService.class);
                  stopService(intent);
              }
          });
  
      }
  
    }
  </code></pre>
  
  <p><strong>And in the Service:</strong></p>
  
  <pre><code>@Override
  public int onStartCommand(final Intent intent, int flags, int startId) {
  
  
      //Creating new thread for my service
      //Always write your long running tasks in a separate thread, to avoid ANR
      new Thread(new Runnable() {
  
  
          @Override
          public void run() {
  
  
  
              //Your logic that service will perform will be placed here
              //In this example we are just looping and waits for 1000 milliseconds in each loop.
              for (int i = 0; i &lt; 2; i++) {
                  try {
                      Thread.sleep(20000);
                  } catch (Exception e) {
                  }
  
                  if(isRunning) {
  
                          Log.v("WORKS!!!!!!!!!!!!!!", " url");
  
                  }
              }
  
              //Stop service once it finishes its task
              stopSelf();
          }
      }).start();
  
      return Service.START_STICKY;
  }
  </code></pre>