Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data from Activity to JobService

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;
    }
}
like image 888
Amrita Stha Avatar asked Sep 08 '17 08:09

Amrita Stha


People also ask

How pass data from activity to services in Android?

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.

What is Jobservice?

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.

What is on start job called?

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).


1 Answers

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");
}
like image 128
Tim Avatar answered Oct 03 '22 01:10

Tim