I've implemented a JobService
and need to capture the user's current location, but implementation requires a "Context" or "Activity". But it is a job processing and I do not have an open Context or Activity.
The doInBackground
method runs from time to time ....
How to implement? Follow my JobService
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.util.Log;
import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;
public class JobSchedulerService extends JobService {
public static final String TAG = "LOG";
@Override
public boolean onStartJob(final JobParameters job) {
// TODO salva as coordenadas no banco local
Log.i(TAG, "Executando");
@SuppressLint("StaticFieldLeak") AsyncTask backgroundTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] objects) {
// TODO envia os dados pro banco na internet
Log.i(TAG, "Executando background");
return null;
}
@Override
protected void onPostExecute(Object o) {
jobFinished(job, true);
}
};
backgroundTask.execute();
return true; //Ainda há algo a ser executado?
}
@Override
public boolean onStopJob(com.firebase.jobdispatcher.JobParameters job) {
// Se houver erro, essa função será chamada
// return true para tentar novamente
return true;
}
}