title: How to stop a thread in a service?after stopService method called? in Android
body: <h2>Image Upload Using Service</h2>
  
  <p>In my app I am uploading an image to a server,I have used background service to do this,the upload is executed in another thread in service.I have read that ,Service runs on the UI thread and the thread in a service is another process,What I need is,I want to cancel the upload when stopService called by a button click.So I want to kill that thread,I tried this code but it is not working properly.Can anybody help?please?</p>
  
  <pre><code>    private void uploadPhoto(Bitmap   bitmap) {
      //in this method you upload the photo to the server: omitted for brevity
      Log.e("Method", "uploadPhoto called");
      final Bitmap bit = bitmap;
      flag=true;
  
             uploadthread = new Thread(new Runnable() {
  
                      @Override
                      public void run() {
  
                          Log.e("While", "Inside while loop");
                          try {
  
                              while (true) {
  
                                  if (flag) {
                                      Log.e("IF", "Inside IF condition"+flag);
                                       return;
  
                                      //uploadthread.destroy();
                                  }
                                  handler.sendEmptyMessage(0);
                                  // t.sleep(5000);
                                  // Toast.makeText(getApplicationContext(), "Horas",
                                  // Toast.LENGTH_LONG).show();
                                  Log.e("Upload", "Upload Started inside Thread");
  
                                  HttpClient httpClient = new DefaultHttpClient();
                                  HttpContext localContext = new BasicHttpContext();
                                  HttpPost httpPost = new HttpPost(Config.FILE_UPLOAD_URL);
  
                                  MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
  
  
                                  ByteArrayOutputStream bos = new ByteArrayOutputStream();
                                  bit.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                                  byte[] data = bos.toByteArray();
  
  
                                  entity.addPart("uploaded_file", new ByteArrayBody(data,
                                          "myImage.jpg"));
  
                                  httpPost.setEntity(entity);
  
                                  HttpResponse response = httpClient.execute(httpPost,
                                          localContext);
                                  BufferedReader reader = new BufferedReader(
                                          new InputStreamReader(
                                                  response.getEntity().getContent(), "UTF-8"));
  
                                  StringBuilder builder = new StringBuilder();
                                  String aux = "";
  
                                  while ((aux = reader.readLine()) != null) {
                                      builder.append(aux);
                                  }
  
                                  String sResponse = builder.toString();
                                  handler.sendEmptyMessage(0);
  
                              }
                              }
                              catch(Exception e){
                                  e.printStackTrace();
                              }
  
  
                            //  stopSelf();
                          }
  
  
                  });
                  uploadthread.start();
                }
  </code></pre>
  
  <p>onDestroy Method</p>
  
  <pre><code> @Override
  public void onDestroy() {
   try {
          handler.removeCallbacks(uploadthread);
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
      EndNotification();
      flag = false;
      Log.e("Service", "Service Destroyed");
      Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
      super.onDestroy();
  
  
  }
  </code></pre>