quentin7b / android-location-tracker

Android helper that tracks user location
MIT License
354 stars 123 forks source link

Not working in WorkManager #27

Open waqarv712 opened 4 years ago

waqarv712 commented 4 years ago

I am using this code:

private void getLocationUpdates() {

            TrackerSettings settings =
                    new TrackerSettings()
                            .setUseGPS(true)
                            .setUseNetwork(true)
                            .setUsePassive(true);
//                            .setTimeBetweenUpdates(5000);
//                            .setTimeBetweenUpdates(30 * 60 * 1000)
//                            .setMetersBetweenUpdates(100);

            LocationTracker tracker = new LocationTracker(getApplicationContext(), settings) {
                @Override
                public void onLocationFound(Location location) {
                    // Do some stuff

                    String latlng = location.getLatitude() + ", " + location.getLongitude();

                    Log.d("WorkManager", "onLocationFound: " + latlng);

                }

                @Override
                public void onTimeout() {
                    Log.d("WorkManager", "onTimeout: ");
                }
            };
            tracker.startListening();
    }

in the doWrok() function of WorkManager. But this code works in the Service.

quentin7b commented 4 years ago

The tracked is based on the android.location.LocationManager class. So there is no reference on any service of any kind in the library. I'm not familiar with the WorkManager yet, but if there a chance of the tracked being destroyed after the dowork call ?

pioneersingh321 commented 4 years ago

in doWork() function you can make BroadcastReceiver.

Intent intent = new Intent(context, LocationBroadcastReceiver.class); context.sendBroadcast(intent);

and call the library functions in BroadcastReceiver

hope it will work .. i have not tried but i have seen in some examples

ballerburg9005 commented 1 year ago

WorkManager is used for battery-friendly background tasks, and it is the normal and only clean way to run background processes.

If you want to "track" your location, it mainly makes sense to do so in the background. So support for this should be very high priority.