1

Activity Code

JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        ComponentName componentName = new ComponentName(this, EJobService.class);
        JobInfo info = new JobInfo.Builder(123, componentName)
                .setRequiresCharging(true)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
                .setPersisted(true)
                .setPeriodic(5*1000,500)
                .build();

        int resultCode = scheduler.schedule(info);
        if (resultCode == JobScheduler.RESULT_SUCCESS) {
            Log.d("Job Started", "Job scheduled");
        } else {
            Log.d("Tag", "Job scheduling failed");
        }

Service code

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class EJobService extends JobService {
    private static final String TAG = "EJobService";
    private boolean jobCancelled = false;

    @Override
    public boolean onStartJob(JobParameters params) {
       getLocation();
        return true;
    }

    private void doBackgroundWork(final JobParameters params) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    Log.d(TAG, "run: " + i);
                    if (jobCancelled) {
                        return;
                    }

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                Log.d(TAG, "Job finished");
                jobFinished(params, false);
            }
        }).start();
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d(TAG, "Job cancelled before completion");
        jobCancelled = true;
        return true;
    }
   public void getLocation() {
    LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    try {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, this);
    } catch (SecurityException e) {
        e.printStackTrace();
    }
}

AndroidManifest.Xml

<service
    android:name=".EJobService"
    android:permission="android.permission.BIND_JOB_SERVICE" />

I am using this code for job scheduler service but the problem is service is not running please tell me what is the problem. i have used this in the Oreo version and this is working fine so please tell me what is the problem.

Getlocation(); crashes app

  • what is exact problem? post logcat here,what is your requirement? – Quick learner Sep 27 '19 at 09:11
  • there is no error showing even in the logcat no app crash no output from service. service not calling from activity. i have tried context changing also. this is working fine in oreo. –  Sep 27 '19 at 09:13
  • 2
    shouldn't the `doBackgroundWork` be inside the onStartJob? – Pemba Tamang Sep 27 '19 at 09:17
  • You have not called you `doBackgroundWork(params)` in your `onStartJob` callback – nitinkumarp Sep 27 '19 at 09:19
  • i did that, after that i removed doBackgroundWork and tried to show a toast. but not showing toast neither method worked. This worked fine in oreo version. but i don't know why this same code not working in androidx. –  Sep 27 '19 at 10:12
  • public void getLocation() { LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); try { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, this); } catch (SecurityException e) { e.printStackTrace(); } } This method crashing app in service @nitinkumarp –  Sep 27 '19 at 11:44
  • Sorry for late reply. What's the crash log? Have asked for the location permission before using above code? You need to ask for location permission when you are scheduling your job. Also check for location permission before calling `getLocation()` – nitinkumarp Oct 01 '19 at 07:06

1 Answers1

1

your code is perfectly working just go and check output in the console.

Rover
  • 661
  • 2
  • 18
  • 39