I want to get lat and longitude value from Activity class to JobService. How can I do that? I'd tried using Intent with putExtras etc(please have a look at the code below) but could not make it right.
MainActivity.class
protected void createLocationRequest(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(final LocationResult locationResult) {
Log.i("onLocationResult", locationResult + "");
latitude = locationResult.getLastLocation().getLatitude() + "";
longitude = locationResult.getLastLocation().getLongitude() + "";
Log.e("onLocationResult lat", latitude);
Log.e("onLocationResult Lon", longitude);
//I need to send latitude and longitude value to jobService?
//how to do that?
//tried using intent but didn't seem to work
//Intent mIntent = new Intent();
//mIntent.putExtra("lat", latitude);
//mIntent.putExtra("lon", longitude);
}
}, null);
}
MyJobService class
public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters jobParameters) {
//I need to get latitude and longitude value here from mainActivity
return true;
}
}
Explanation. Using putExtra() method, we can send the data. While using it, we need to call setResult() method in services. We can also store data in a common database and access it on services as well as in Activity.
It's a new type of service, that is invoked for tasks that are scheduled to be run depending on system conditions (e.g. idle, plugged in). Entry point for the callback from the JobScheduler. This is the base class that handles asynchronous requests that were previously scheduled.
OnStartJob is called by the Android system when it starts your job. However, onStopJob is only called if the job was cancelled before being finished (e.g. we require the device to be charging, and it gets unplugged).
Where you construct the JobInfo object, you use setExtras() to pass a PersistableBundle of extras.
ComponentName componentName = new ComponentName(context, MyJobService.class);
PersistableBundle bundle = new PersistableBundle();
bundle.putLong("lat", lat);
bundle.putLong("lon", lon);
JobInfo jobInfo = new JobInfo.Builder(0, componentName)
.setExtras(bundle)
.build();
Then in your JobService you can retrieve them with
@Override
public boolean onStartJob(JobParameters params) {
params.getExtras().getLong("lat");
params.getExtras().getLong("lon");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With