title: Android Studio, Java. JobIntentService stops after i try to use method for getting device location
body: <p>Okay, so I'm trying to make a service that records sound amplitude, time, date and device location every 5 seconds. It records sound, time and date perfectly but the service seems to crash after I use updateGPS() method that you will see in the code (service stops, but app doesn't crash). As somebody who doesnt have much experience in coding, I would appreciate any help on how to fix this.</p>
  <p>This is the code for service:</p>
  <pre><code>public class MyService extends JobIntentService {
  
      static void enqueueWork(Context context, Intent work) {
          enqueueWork(context, MyService.class, 123, work);
      }
      FusedLocationProviderClient loc;
      MainActivity ma;
      public double lat;
      public double lon;
      public int x = 5;
      private String date;
      private String time;
      private MediaRecorder rec;
  
  
      private String recFile() {
          ContextWrapper cw = new ContextWrapper(getApplicationContext());
          File soundDir = cw.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
          File file = new File(soundDir, &quot;recFile&quot; + &quot;.mp3&quot;);
          return file.getPath();
      }
  
  
      @Override
      public void onCreate() {
          super.onCreate();
  
      }
  
      @Override
      protected void onHandleWork(@NonNull Intent intent) {
          String input = intent.getStringExtra(&quot;inputExtra&quot;);
          try {
              rec = new MediaRecorder();
              rec.setAudioSource(MediaRecorder.AudioSource.MIC);
              rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
              rec.setOutputFile(recFile());
              rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
              rec.prepare();
              rec.start();
              rec.getMaxAmplitude();
              while (MainActivity.b == true) {
                  for (int i = 0; i &lt; x; i++) {
                      if (isStopped()) return;
                      SystemClock.sleep(1000);
                  }
                  updateGPS();
                  date = new SimpleDateFormat(&quot;dd-MM-yyyy&quot;, Locale.getDefault()).format(new Date());
                  time = new SimpleDateFormat(&quot;HH:mm:ss&quot;, Locale.getDefault()).format(new Date());
                  int test = rec.getMaxAmplitude();
                  Log.i(&quot;tag&quot;, &quot;jacina: &quot; + test);
                  Log.i(&quot;t&quot;, &quot;datum: &quot; + date);
                  Log.i(&quot;t1&quot;, &quot;vreme: &quot; + time);
                  Log.i(&quot;t2&quot;, &quot;latitude: &quot; + lat);
                  Log.i(&quot;t3&quot;, &quot;longitude: &quot; + lon);
  
              }
              rec.stop();
              rec.release();
              rec = null;
              deleteFile(recFile());
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
      }
  
      @Override
      public boolean onStopCurrentWork() {
          return super.onStopCurrentWork();
      }
  
      private void updateValues(Location location) {
          lat = location.getLatitude();
          lon = location.getLongitude();
      }
  
      private void updateGPS() {
          loc = LocationServices.getFusedLocationProviderClient(MyService.this);
          if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
              return;
          }else{
              if(ma != null){
                  ma.runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          Toast.makeText(MyService.this, &quot;location not available&quot;, Toast.LENGTH_SHORT).show();
                      }
                  });
              }
          }
          loc.getLastLocation().addOnSuccessListener((Executor) this, new OnSuccessListener&lt;Location&gt;() {
              @Override
              public void onSuccess(Location location) {
                  updateValues(location);
              }
          });
      }
  
  
  }
  </code></pre>
  <p>This is only part of code that I think needs to be changed:</p>
  <pre><code>loc.getLastLocation().addOnSuccessListener((Executor) this, new OnSuccessListener&lt;Location&gt;() {
              @Override
              public void onSuccess(Location location) {
                  updateValues(location);
              }
          });
  </code></pre>
  <p>And this is logcat message:</p>
  <pre><code>2022-01-25 16:43:14.304 29189-29595/com.example.projekat W/System.err: java.lang.ClassCastException: com.example.projekat.MyService cannot be cast to java.util.concurrent.Executor
  2022-01-25 16:43:14.306 29189-29595/com.example.projekat W/System.err:     at com.example.projekat.MyService.updateGPS(MyService.java:128)
  2022-01-25 16:43:14.306 29189-29595/com.example.projekat W/System.err:     at com.example.projekat.MyService.onHandleWork(MyService.java:78)
  2022-01-25 16:43:14.306 29189-29595/com.example.projekat W/System.err:     at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:391)
  2022-01-25 16:43:14.307 29189-29595/com.example.projekat W/System.err:     at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:382)
  2022-01-25 16:43:14.307 29189-29595/com.example.projekat W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
  2022-01-25 16:43:14.308 29189-29595/com.example.projekat W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  2022-01-25 16:43:14.308 29189-29595/com.example.projekat W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
  2022-01-25 16:43:14.308 29189-29595/com.example.projekat W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
  2022-01-25 16:43:14.309 29189-29595/com.example.projekat W/System.err:     at java.lang.Thread.run(Thread.java:764)
  </code></pre>
  <p>Thanks in advance.</p>