title: Android stop start_sticky service after finishing the task
body: <p>I am trying to create a service after the application is killed and wait for the mobile to connect to wifi after that finish the task and stop the service, it works fine when I send the task while the app is not killed but after I kill the app the service wont quit</p>
  
  <p>I've got three classes, the main class where I start the service </p>
  
  <pre><code>      public class MainActivity extends AppCompatActivity {
  
      //----------------------USER FIELDS --------------------&gt;
  
      Spinner Category;
      EditText Description;
      EditText Address;
      EditText Date;
      EditText Time;
      EditText Name;
      EditText Email;
      EditText Phone;
  
      //-----------------------CONSTANTS----------------------&gt;
  
      private static final int NUM_PAGES = 5;
      private static final int CAMERA_VALUE = 301;
  
      //----------------------IMAGE DIRECTORY-----------------&gt;
  
      String root;
      String imageFolderPath;
      String imageName;
      Uri fileUri;
      ArrayList&lt;Uri&gt; fileUris = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; filepaths;
      ArrayList&lt;String&gt; photoPaths;
      List&lt;String&gt; Images = new ArrayList&lt;&gt;();
  
      //----------------------FRAGMENTS-----------------------&gt;
  
      SplashFragment splashFragment = new SplashFragment();
      DescFragment descFragment = new DescFragment();
      InfoFragment infoFragment = new InfoFragment();
      ChooserFragment chooserFragment = new ChooserFragment();
      SuccessFragment successFragment = new SuccessFragment();
  
      //-------------------DATABASE HANDLER-------------------&gt;
  
      DatabaseHandler db = new DatabaseHandler(this);
  
      //---------------------DIALOGS--------------------------&gt;
  
      Dialog infoDialog;
      Dialog languageDialog;
  
      //-----------------SHARED PREFS-------------------------&gt;
  
      SharedPreferences prefs = null;
  
      //--------------IMAGEVIEW PREVIEWS----------------------&gt;
  
      ArrayList&lt;ImageView&gt; img = new ArrayList&lt;&gt;();
  
      //-----------------COUNTERS-----------------------------&gt;
  
      private static int image_counter = 0;
  
      //----------VIEWPAGER AND VIEWPAGER ADAPTER-------------&gt;
      private ViewPager mPager;
      private PagerAdapter mPagerAdapter;
  
  
      TextInputLayout description_layout;
      TextInputLayout spinner_layout;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          //-------------CHECK FOR PERMISSIONS----------------&gt;
  
          if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
          {
              ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 0);
          }
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          findViewById(R.id.mainLayout).requestFocus();
  
          mPager = (ViewPager) findViewById(R.id.pager);
          mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
          mPager.setAdapter(mPagerAdapter);
  
          prefs = getSharedPreferences("co.milingona.socialactivist", MODE_PRIVATE);
  
          selectLanguge(prefs.getString("language","sq"), false);
  
          if(prefs.getBoolean("firstTimeRunning",true))
          {
              createShortcut();
              prefs.edit().putBoolean("firstTimeRunning",false).commit();
          }
      }
      @Override
      protected void onResume()
      {
          super.onResume();
      }
  
      private void restartActivity()
      {
          Intent intent = getIntent();
          finish();
          startActivity(intent);
      }
      private void selectLanguge(String language, boolean restart)
      {
          prefs.edit().putString("language", language).commit();
  
          String languageToLoad  = language; // your language
          Locale locale = new Locale(languageToLoad);
          Locale.setDefault(locale);
          Configuration config = new Configuration();
          config.setLocale(locale);
          getBaseContext().getResources().updateConfiguration(config,
                  getBaseContext().getResources().getDisplayMetrics());
  
          if( restart == true ) {
              restartActivity();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      {
              getMenuInflater().inflate(R.menu.menu_main, menu);
              return true;
      }
  
      @Override
      public void onBackPressed() {
          if (mPager.getCurrentItem() == 0) {
              super.onBackPressed();
          }
          else if(mPager.getCurrentItem() == 4)
          {
              restartActivity();
          } else {
              mPager.setCurrentItem(mPager.getCurrentItem() - 1);
          }
      }
  
  
      public void splash_raporto(View view)
      {
          mPager.setCurrentItem(mPager.getCurrentItem() + 1);
      }
  
      public void desc_prev(View view)
      {
  
          if (view != null) {
              InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
          }
          mPager.setCurrentItem(mPager.getCurrentItem() - 1);
  
      }
      public void desc_next(View view)
      {
          description_layout= (TextInputLayout) findViewById(R.id.description_layout);
          spinner_layout= (TextInputLayout) findViewById(R.id.spinner_layout);
  
          boolean continuePager = true;
  
          Category = (Spinner)findViewById(R.id.category);
          Description = (EditText)findViewById(R.id.description);
  
          if(Description.getText().toString().trim().length() == 0)
          {
              description_layout.setError(getText(R.string.description));
              continuePager=false;
  
          } else {
  
              description_layout.setErrorEnabled(false);
          }
  
  
  
          if(Category.getSelectedItem().toString().trim() == "Zgjidhni Kategorinë")
          {
              spinner_layout.setError(getText(R.string.category));
              continuePager=false;
          }
  
          if(continuePager == true)
          {
              mPager.setCurrentItem(mPager.getCurrentItem()+1);
  
              if (view != null) {
                  InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
              }
          }
  
      }
  
      public void info_prev(View view)
      {
          mPager.setCurrentItem(mPager.getCurrentItem()-1);
      }
  
      public void info_next(View view)
      {
          Address = (EditText)findViewById(R.id.address);
          this.Date = (EditText)findViewById(R.id.date);
          Time = (EditText)findViewById(R.id.time);
          Name = (EditText)findViewById(R.id.name);
          Email = (EditText)findViewById(R.id.email);
          Phone = (EditText)findViewById(R.id.phone);
  
          if(Email.getText().toString().trim().length()!=0)
          {
              if(isValidEmail(Email.getText().toString()))
              {
                  mPager.setCurrentItem(mPager.getCurrentItem()+1);
              }
              else{
                  Email.setBackground(getResources().getDrawable(R.drawable.border_bottom_red));
              }
          }
          else {
              mPager.setCurrentItem(mPager.getCurrentItem() + 1);
              Email.setBackground(getResources().getDrawable(R.drawable.border_bottom_white));
          }
  
          if (view != null) {
              InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
          }
      }
  
      public void choose_dergo(View view)
      {
          mPager.setCurrentItem(mPager.getCurrentItem()+1);
  
          for (Uri uri: fileUris) {
              Images.add(uri.toString());
          }
  
         db.addReport(new Report(Category.getSelectedItem().toString(),Description.getText().toString(),"Mitrovice", Address.getText().toString(),this.Date.getText().toString() + " " + Time.getText().toString(), Name.getText().toString(), Email.getText().toString(), Phone.getText().toString(), Images.toArray(new String[Images.size()])));
  
          if(CheckConnectivityService.running==false)
          {
              Intent stickyService=new Intent(this, CheckConnectivityService.class);
              startService(stickyService);
              CheckConnectivityService.running=true;
          }
      }
  
      public void camera_intent(View view)
      {
         if(image_counter&lt;5)
         {
             root = Environment.getExternalStorageDirectory()
                     + "/SocialAcitivist";
             imageFolderPath = root + "/Images";
             File imagesFolder = new File(imageFolderPath);
             imagesFolder.mkdirs();
             Date d = new Date();
             CharSequence s = DateFormat.format("hh-mm-ss", d.getTime());
             imageName = "img-" + s + ".jpg";
             File image = new File(imageFolderPath, imageName);
             fileUri = Uri.fromFile(image);
             Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
             takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
             startActivityForResult(takePictureIntent, CAMERA_VALUE);
         }
          else{
             Toast.makeText(this,"First delete some pictures below",Toast.LENGTH_SHORT).show();
         }
      }
  
      public void gallery_intent(View view)
      {
          FilePickerBuilder.getInstance().setMaxCount(5-image_counter)
                  .setSelectedFiles(filepaths)
                  .setActivityTheme(R.style.AppTheme)
                  .pickPhoto(this);
      }
  
      protected void onActivityResult(int requestCode, int resultCode, Intent data)
      {
  
          if (requestCode == CAMERA_VALUE) {
              fileUris.add(fileUri);
              image_counter++;
          }
  
          if (requestCode == FilePickerConst.REQUEST_CODE_PHOTO &amp;&amp; resultCode == RESULT_OK &amp;&amp; data != null)
          {
              photoPaths = new ArrayList&lt;&gt;();
              photoPaths.addAll(data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_PHOTOS));
  
              for (String photopath : photoPaths)
              {
                  fileUris.add(Uri.fromFile(new File(photopath)));
                  image_counter++;
              }
          }
  
          img.add(0, (ImageView)findViewById(R.id.prev1));
          img.add(1, (ImageView)findViewById(R.id.prev2));
          img.add(2, (ImageView)findViewById(R.id.prev3));
          img.add(3, (ImageView)findViewById(R.id.prev4));
          img.add(4, (ImageView)findViewById(R.id.prev5));
  
          int img_counter=0;
  
          for(Uri uri:fileUris)
          {
              img.get(img_counter).setImageURI(uri);
              img_counter++;
          }
      }
  
      public void showTimePickerDialog(View v)
      {
          DialogFragment newFragment = new TimePickerFragment();
          newFragment.show(getSupportFragmentManager(), "timePicker");
      }
  
      public void showDatePickerDialog(View v)
      {
          DialogFragment newFragment = new DatePickerFragment();
          newFragment.show(getSupportFragmentManager(), "datePicker");
      }
      public void OpenInformation(MenuItem item)
      {
          infoDialog=new Dialog(this,R.style.AppTheme_Dark);
          infoDialog.setContentView(R.layout.infromation_layout);
          infoDialog.show();
      }
  
      public void close_info_dialog(View view)
      {
          infoDialog.dismiss();
      }
  
      public void OpenLanguages(MenuItem item)
      {
          languageDialog=new Dialog(this, R.style.AppTheme_Dark_Dialog);
          languageDialog.setContentView(R.layout.language_layout);
          languageDialog.show();
      }
  
      public void ChangeLanguage(View view)
      {
          switch (view.getId())
          {
              case R.id.sq:
                  if( prefs.getString("language","en").equalsIgnoreCase("en"))
                  {
                      selectLanguge("sq", true);
                  }
                  break;
              case R.id.en:
                  if( prefs.getString("language","sq").equalsIgnoreCase("sq"))
                  {
                      selectLanguge("en", true);
                  }
                  break;
          }
  
          languageDialog.dismiss();
      }
  
      public void removeItem(View view)
      {
          int img_counter=0;
  
          try{
              switch (view.getId())
              {
                  case R.id.prev1:
  
                      fileUris.remove(0);
                      for(Uri uri:fileUris)
                      {
                          img.get(img_counter).setImageURI(uri);
                          img_counter++;
  
                      }
                      img.get(img_counter).setImageURI(null);
  
                      break;
                  case R.id.prev2:
  
                      fileUris.remove(1);
                      for(Uri uri:fileUris)
                      {
                          img.get(img_counter).setImageURI(uri);
                          img_counter++;
  
                      }
                      img.get(img_counter).setImageURI(null);
  
                      break;
                  case R.id.prev3:
  
                      fileUris.remove(2);
                      for(Uri uri:fileUris)
                      {
                          img.get(img_counter).setImageURI(uri);
                          img_counter++;
  
                      }
                      img.get(img_counter).setImageURI(null);
  
                      break;
                  case R.id.prev4:
  
                      fileUris.remove(3);
                      for(Uri uri:fileUris)
                      {
                          img.get(img_counter).setImageURI(uri);
                          img_counter++;
  
                      }
                      img.get(img_counter).setImageURI(null);
  
                      break;
                  case R.id.prev5:
  
                      fileUris.remove(4);
                      for(Uri uri:fileUris)
                      {
                          img.get(img_counter).setImageURI(uri);
                          img_counter++;
  
                      }
                      img.get(img_counter).setImageURI(null);
  
  
                      break;
              }
              image_counter--;
          }
          catch(Exception e){}
  
      }
  
      public void openURL(View view) {
          String url;
          switch (view.getId())
          {
              case R.id.facebook: url="https://www.facebook.com";
                  break;
              case R.id.twitter: url="https://www.twitter.com";
                  break;
              case R.id.wordpress: url="https://www.facebook.com";
                  break;
              default: url="https://www.facebook.com";
                  break;
          }
          Intent i = new Intent(Intent.ACTION_VIEW);
          i.setData(Uri.parse(url));
          startActivity(i);
      }
  
      public static class TimePickerFragment extends DialogFragment
              implements TimePickerDialog.OnTimeSetListener
      {
          @Override
          public Dialog onCreateDialog(Bundle savedInstanceState)
          {
              final Calendar c = Calendar.getInstance();
              int hour = c.get(Calendar.HOUR_OF_DAY);
              int minute = c.get(Calendar.MINUTE);
  
              return new TimePickerDialog(getActivity(), R.style.AppTheme_Dark_Dialog, this, hour, minute,
                      DateFormat.is24HourFormat(getActivity()));
          }
  
  
          public void onTimeSet(TimePicker view, int hourOfDay, int minute)
          {
              EditText editTime=(EditText)getActivity().findViewById(R.id.time);
              editTime.setText(hourOfDay+":"+minute);
          }
      }
  
      public static class DatePickerFragment extends DialogFragment
              implements DatePickerDialog.OnDateSetListener
      {
  
          @Override
          public Dialog onCreateDialog(Bundle savedInstanceState)
          {
              final Calendar c = Calendar.getInstance();
              int year = c.get(Calendar.YEAR);
              int month = c.get(Calendar.MONTH);
              int day = c.get(Calendar.DAY_OF_MONTH);
              DatePickerDialog dialog = new DatePickerDialog(getActivity(), R.style.AppTheme_Dark_Dialog, this, year, month, day);
              dialog.getDatePicker().setMaxDate(new Date().getTime());
  
              return dialog;
          }
  
  
          public void onDateSet(DatePicker view, int year, int month, int day)
          {
              EditText editDate=(EditText)getActivity().findViewById(R.id.date);
              editDate.setText(year+"-"+month+"-"+day);
          }
      }
  
      private boolean isValidEmail(String email)
      {
          String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                  + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  
          Pattern pattern = Pattern.compile(EMAIL_PATTERN);
          Matcher matcher = pattern.matcher(email);
          return matcher.matches();
      }
  
      private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
      {
  
          public ScreenSlidePagerAdapter(FragmentManager fm)
          {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int position)
          {
              switch (position)
              {
                  case 0: return splashFragment;
                  case 1: return descFragment;
                  case 2: return infoFragment;
                  case 3: return chooserFragment;
                  case 4: return successFragment;
                  default: return splashFragment;
              }
          }
  
          @Override
          public int getCount()
          {
              return NUM_PAGES;
          }
      }
  
      private void createShortcut()
      {
          final Intent shortcutIntent = new Intent(this, MainActivity.class);
          final Intent intent = new Intent();
          intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
          intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
          intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher));
          intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
          sendBroadcast(intent);
      }
  }
  </code></pre>
  
  <p>then I got the service class</p>
  
  <pre><code>public final class CheckConnectivityService extends IntentService {
      Context context = this;
      NetworkConnectivityCheck networkConnectivityCheck = new NetworkConnectivityCheck();
      public Thread backgroundThread;
      public static boolean running = false;
      public static boolean stop = false;
      public static Intent _intent;
      public CheckConnectivityService() {
          super("S");
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {
          _intent=intent;
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          _intent=intent;
          networkConnectivityCheck.register(context);
          this.backgroundThread.start();
          running = false;
          return START_STICKY;
      }
  
      @Override
      public void onDestroy() {
          networkConnectivityCheck.unregister(context);
          backgroundThread.interrupt();
      }
  
      @Override
      public void onCreate() {
          this.backgroundThread = new Thread(myTask);
  
      }
  
      private Runnable myTask = new Runnable() {
          public void run() {
              while (running == true) {
  
                      if (stop == true) {
                          stop = false;
                          stopSelf();
                      }
                  }
              }
          };
      }
  </code></pre>
  
  <p>and my third class where I check if internet is available if yes upload the form and make the service stop</p>
  
  <pre><code>public class NetworkConnectivityCheck {
  
      public boolean internetAvailable = false;
      private BroadcastReceiver networkChangeReceiver;
      List&lt;Report&gt; reports;
  
      NetworkConnectivityCheck(){
          this.networkChangeReceiver = new BroadcastReceiver()
          {
              @Override
              public void onReceive(Context context, Intent intent) {
                  int networkState = NetworkUtil.getConnectionStatus(context);
                  DatabaseHandler db=new DatabaseHandler(context);
                  if(networkState == NetworkUtil.NOT_CONNECTED){
                      internetAvailable = false;
  
  
                  } else if(networkState == NetworkUtil.MOBILE){
                      internetAvailable = true;
                      //MainActivity.tvStatus.setText("ONLINE"); // you do something here.
                  } else if(networkState == NetworkUtil.WIFI){
  
                      internetAvailable = true;
  
                          if(db.getReportsCount()!=0){
                              reports=db.getAllReports();
                              for(Report report : reports){
                                  Upload upload=new Upload(report);
                                  Thread doInBackground = new Thread(upload);
                                  doInBackground.start();
                              }
  
                              db.deleteAll();
                              CheckConnectivityService.running=true;
                              CheckConnectivityService.stop=true;
  
                      }
                  }
              }
          };
      }
      public void register(Context context)
      {
          context.registerReceiver(networkChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
      }
      public void unregister(Context context)
      {
          context.unregisterReceiver(networkChangeReceiver);
      }
  
      public class Upload implements Runnable
      {
          Report report;
          public Upload(Report _report)
          {
              report=_report;
          }
          @Override
          public void run() {
              try {
                  MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");
  
                  OkHttpClient client = new OkHttpClient.Builder()
                          .connectTimeout(30, TimeUnit.SECONDS)
                          .writeTimeout(120, TimeUnit.SECONDS)
                          .readTimeout(20, TimeUnit.SECONDS)
                          .build();
  
                  MultipartBody.Builder mRequestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
  
                  mRequestBody.addFormDataPart("category", report.getCategory());
                  mRequestBody.addFormDataPart("description", report.getDescription());
                  mRequestBody.addFormDataPart("city", report.getCity());
                  mRequestBody.addFormDataPart("address", report.getAddress());
                  mRequestBody.addFormDataPart("datetime", report.getDateTime());
                  mRequestBody.addFormDataPart("name", report.getName());
                  mRequestBody.addFormDataPart("email", report.getEmail());
                  mRequestBody.addFormDataPart("phone", report.getPhone());
  
                  if(report.getImages()[0].trim().length()!=0) {
                      ArrayList&lt;Uri&gt; fileUris = new ArrayList&lt;&gt;();
                      for (String uri : report.getImages())
                      {
                          fileUris.add(Uri.parse(uri));
                      }
  
                      for (Uri FileUri : fileUris)
                      {
                          File file = new File(FileUri.getPath());
  
                          RequestBody imageBody = RequestBody.create(MEDIA_TYPE_JPEG, file);
                          mRequestBody.addFormDataPart("images[]", FileUri.getLastPathSegment(), imageBody);
                      }
                  }
                  RequestBody requestBody = mRequestBody.build();
  
                  Request request = new Request.Builder()
                          .addHeader("Content-Type","text/json; Charset=UTF-8")
                          .header("Authorization", "Basic bWlsaW5nb25hOlN0")
                          .url("http://LINK.com")
                          .post(requestBody)
                          .build();
  
                  Response response = client.newCall(request).execute();
  
  
              } catch (Exception e)
              {
                  e.printStackTrace();
              }
  
              Thread.currentThread().interrupt();
  
              return;
          }
  
      }
  }
  </code></pre>
  
  <p>and here is the NetworkUtil class in case u want to understand it more</p>
  
  <pre><code>public class NetworkUtil
  {
      public static final int NOT_CONNECTED = 0;
      public static final int WIFI = 1;
      public static final int MOBILE = 2;
      public static int getConnectionStatus(Context context)
      {
          ConnectivityManager connectivityManager =
                  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  
          if(networkInfo != null)
          {
              if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
              {
                  return WIFI;
              }
  
              if(networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
              {
                  return MOBILE;
              }
          }
          return NOT_CONNECTED;
      }
  }
  </code></pre>