Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can wait until GPS catch location #191

Open
Isma3ilMohamed opened this issue Aug 4, 2018 · 2 comments
Open

how can wait until GPS catch location #191

Isma3ilMohamed opened this issue Aug 4, 2018 · 2 comments

Comments

@Isma3ilMohamed
Copy link

hey everyone
i really like this dependency because it save my time
but i want to know how to use it in a service
and how can make it wait until gps detect device location
if i enable gps for first time
thanks

@cyrixmorten
Copy link
Contributor

cyrixmorten commented Aug 4, 2018 via email

@Isma3ilMohamed
Copy link
Author

hey @cyrixmorten
i try this code as you said
but it give me null pointer exception
`public class GPSTracker extends Service {

private Context context;
LocationRequest request;
ReactiveLocationProvider locationProvider;

private Location location;

private CompositeDisposable disposable = new CompositeDisposable();


@SuppressLint("MissingPermission")
public GPSTracker(Context context) {
    this.context = context;
    location = ((LocationManager)context.getSystemService(LOCATION_SERVICE))
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    setUpLocation(context);
}


public Location getLocation() {
    return location;
}

public void setLocation(Location location) {
    this.location = location;
}

@SuppressLint("MissingPermission")
private void setUpLocation(final Context context) {
    request = LocationRequest.create() //standard GMS LocationRequest
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setNumUpdates(5)
            .setInterval(100);
    locationProvider =
            new ReactiveLocationProvider(context, ReactiveLocationProviderConfiguration
                    .builder()
                    .setRetryOnConnectionSuspended(true)
                    .build()
            );

    disposable.add(
            locationProvider.getUpdatedLocation(request)

                    .filter(new Predicate<Location>() {
                        @Override
                        public boolean test(Location location) throws Exception {
                            if (location != null) {
                                Timber.e("Location updated");
                                return true;
                            }
                            return false;
                        }
                    })

                    .subscribe(new Consumer<Location>() {
                        @Override
                        public void accept(Location location) throws Exception {
                         setLocation(location);
                        }
                    })
    );


}


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
    disposable.dispose();
}

}`

i don't know why

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants