How to call inbuilt method of location listener in another class

173 Views Asked by At

I am using location listener to track the location.I implement the location listener, there are inbuilt methods of location listener such as onLocationChanged,OnstatusChanged,onProviderEnabled etc.Whenerver the location changed it will hit on server but I want to do at every 10 min of interval So I am using timer for that after every 10 mins it will hit the location. I cannot call the method inside the timer. So how to do it.

public class LocationMonitoringService  implements LocationListener, GpsStatus.Listener {


    private static final String TAG = LocationMonitoringService.class.getSimpleName();

    private final Context mContext;

    private final LocationRepository mLocationRepository;

    private final ShareLocationAdapterClass mAdapter;

    private final SyncOfflineRepository syncOfflineRepository;

    private long updatedTime = 0;

    private final List<UserLocationPojo> mUserLocationPojoList;

    private final SyncOfflineAttendanceRepository syncOfflineAttendanceRepository;


    private Handler mHandler = new Handler();
    private Timer mTimer = null;
    long notify_interval = 1000 * 60 * 5;


    public LocationMonitoringService(final Context context) {
        mContext = context;

        mLocationRepository = new LocationRepository(AUtils.mainApplicationConstant.getApplicationContext());

        syncOfflineRepository = new SyncOfflineRepository(AUtils.mainApplicationConstant.getApplicationContext());

        syncOfflineAttendanceRepository = new SyncOfflineAttendanceRepository(AUtils.mainApplicationConstant.getApplicationContext());

        mUserLocationPojoList = new ArrayList<>();

        mAdapter = new ShareLocationAdapterClass();

        mAdapter.setShareLocationListener(new ShareLocationAdapterClass.ShareLocationListener() {

            @Override
            public void onSuccessCallBack(boolean isAttendanceOff) {
                if (isAttendanceOff && !syncOfflineAttendanceRepository.checkIsAttendanceIn()) {
                    AUtils.setIsOnduty(false);
                    ((MyApplication) AUtils.mainApplicationConstant).stopLocationTracking();
                }
            }

            @Override
            public void onFailureCallBack() {

            }
        });
    }



    public void onStartTacking() {

        LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);


        //Exception thrown when GPS or Network provider were not available on the user's device.
        try {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
            criteria.setAltitudeRequired(false);
            criteria.setSpeedRequired(true);
            criteria.setCostAllowed(false);
            criteria.setBearingRequired(false);

            //API level 9 and up
            criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
            criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);



            int gpsFreqInDistance =100;

            assert locationManager != null;
            locationManager.addGpsStatusListener(this);


            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, AUtils.LOCATION_INTERVAL,
                    gpsFreqInDistance, this,null);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, AUtils.LOCATION_INTERVAL,
                    gpsFreqInDistance, this,null);



        } catch (IllegalArgumentException | SecurityException e) {
            Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
            Log.d(TAG, "onStartTacking: " + e.getMessage());
            Log.d(TAG, "onStartTacking: " + e.getLocalizedMessage());

            e.printStackTrace();
        } catch (RuntimeException e) {
            Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
            Log.d(TAG, "onStartTacking: " + e.getMessage());
            e.printStackTrace();

        }
    }

    public void onStopTracking(Context context) {
        mAdapter.shareLocation();
        LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        locationManager.removeUpdates(this);
//        cancelAlarm(context, (AlarmManager)context.getSystemService(Context.ALARM_SERVICE));


    }

    /*
     * LOCATION CALLBACKS
     */


    //to get the location change
    @Override
    public void onLocationChanged(Location location) {
        Log.d("okh ", "onLocationChanged:   "+System.currentTimeMillis());


        if (location != null) {

            Log.d(TAG, String.valueOf(location.getAccuracy()));

            if (!AUtils.isNullString(String.valueOf(location.getLatitude())) && !AUtils.isNullString(String.valueOf(location.getLongitude()))) {

                Prefs.putString(AUtils.LAT, String.valueOf(location.getLatitude()));
                Prefs.putString(AUtils.LONG, String.valueOf(location.getLongitude()));

                if (Prefs.getBoolean(AUtils.PREFS.IS_ON_DUTY, false)) {
                    if (updatedTime == 0) {
                        updatedTime = System.currentTimeMillis();
                        Log.d(TAG, "updated Time ==== " + updatedTime);
                    }

                    if ((updatedTime + AUtils.LOCATION_INTERVAL_MINUTES) <= System.currentTimeMillis()) {
                        updatedTime = System.currentTimeMillis();
                        Log.d(TAG, "updated Time ==== " + updatedTime);

                    }

                    mTimer = new Timer();
                    mTimer.schedule(new TimerTaskToGetLocation(), 5,notify_interval);
//                   sendLocation();
                }
            }
        } else {
            Log.d(TAG, "onLocationChanged:  no location found !!");
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d(TAG, "onStatusChanged" + provider + "Status" + status);

    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d(TAG, " onProviderEnabled" + provider);

    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d(TAG, " onProviderDisabled" + provider);
    }


    @Override
    public void onGpsStatusChanged(int event) {

    }



   private void sendLocation() {

        //  mAdapter.shareLocation(getTempList());
        Log.d("okh", "sendLocation: Current Time In Millies "+ System.currentTimeMillis());

        try {
            Calendar CurrentTime = AUtils.getCurrentTime();
            Calendar DutyOffTime = AUtils.getDutyEndTime();

            if (CurrentTime.before(DutyOffTime)) {

                Log.i(TAG, "Before");

                UserLocationPojo userLocationPojo = new UserLocationPojo();

                userLocationPojo.setUserId(Prefs.getString(AUtils.PREFS.USER_ID, ""));
                userLocationPojo.setLat(Prefs.getString(AUtils.LAT, ""));
                userLocationPojo.setLong(Prefs.getString(AUtils.LONG, ""));
                double startLat = Double.parseDouble(Prefs.getString(AUtils.LAT, "0"));
                double startLng = Double.parseDouble(Prefs.getString(AUtils.LONG, "0"));
                userLocationPojo.setDistance(String.valueOf(AUtils.calculateDistance(
                        AUtils.mainApplicationConstant.getApplicationContext(), startLat, startLng)));
//                userLocationPojo.setDatetime(AUtils.getServerDateTime()); //TODO
                userLocationPojo.setDatetime(AUtils.getServerDateTimeLocal());
                userLocationPojo.setOfflineId("0");

                userLocationPojo.setIsOffline(AUtils.isInternetAvailable() && AUtils.isConnectedFast(mContext));

                String UserTypeId = Prefs.getString(AUtils.PREFS.USER_TYPE_ID, AUtils.USER_TYPE.USER_TYPE_GHANTA_GADI);
                if (AUtils.isInternetAvailable()) {
                    TableDataCountPojo.LocationCollectionCount count = syncOfflineRepository.getLocationCollectionCount(AUtils.getLocalDate());
                    if ((UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_GHANTA_GADI) || UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_WASTE_MANAGER))
                            && (count.getLocationCount() > 0 || count.getCollectionCount() > 0)) {
                        syncOfflineRepository.insetUserLocation(userLocationPojo);
                    } else {
                        mUserLocationPojoList.add(userLocationPojo);
                        mAdapter.shareLocation(mUserLocationPojoList);
                        mUserLocationPojoList.clear();
                    }
                } else {
                    if (UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_EMP_SCANNIFY)) {
                        Type type = new TypeToken<UserLocationPojo>() {
                        }.getType();
                        mLocationRepository.insertUserLocationEntity(new Gson().toJson(userLocationPojo, type));
                    } else {
                        syncOfflineRepository.insetUserLocation(userLocationPojo);
                    }
                    mUserLocationPojoList.clear();
                }

            }
            else {
                Log.i(TAG, "After");

                syncOfflineAttendanceRepository.performCollectionInsert(mContext,
                        syncOfflineAttendanceRepository.checkAttendance(), AUtils.getCurrentDateDutyOffTime());

                AUtils.setIsOnduty(false);
                ((MyApplication) AUtils.mainApplicationConstant).stopLocationTracking();

                Activity activity = ((Activity) AUtils.currentContextConstant);

                if (activity instanceof DashboardActivity) {
                    ((Activity) AUtils.currentContextConstant).recreate();
                    AUtils.DutyOffFromService = true;
                }

                if (!AUtils.isNull(AUtils.currentContextConstant)) {
                    ((Activity) AUtils.currentContextConstant).recreate();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }


    }
  


    private class TimerTaskToGetLocation extends TimerTask {
        @Override
        public void run() {
            sendLocation();


        }
    }


}
1

There are 1 best solutions below

2
DeePanShu On

Here is the code please try this:

public class LocationMonitoringService  implements LocationListener, GpsStatus.Listener {


    private static final String TAG = LocationMonitoringService.class.getSimpleName();

    private final Context mContext;

    private final LocationRepository mLocationRepository;

    private final ShareLocationAdapterClass mAdapter;

    private final SyncOfflineRepository syncOfflineRepository;

    private long updatedTime = 0;

    private int gpsFreqInDistance =100;

    private LocationManager locationManager;

    private final List<UserLocationPojo> mUserLocationPojoList;

    private final SyncOfflineAttendanceRepository syncOfflineAttendanceRepository;


    private Handler mHandler = new Handler();
    private Timer mTimer = null;
    long notify_interval = 1000 * 60 * 5;


    public LocationMonitoringService(final Context context) {
        mContext = context;
      
        // here we declare the variable as a field for this class instance
        locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        mLocationRepository = new LocationRepository(AUtils.mainApplicationConstant.getApplicationContext());

        syncOfflineRepository = new SyncOfflineRepository(AUtils.mainApplicationConstant.getApplicationContext());

        syncOfflineAttendanceRepository = new SyncOfflineAttendanceRepository(AUtils.mainApplicationConstant.getApplicationContext());

        mUserLocationPojoList = new ArrayList<>();

        mAdapter = new ShareLocationAdapterClass();

        mAdapter.setShareLocationListener(new ShareLocationAdapterClass.ShareLocationListener() {

            @Override
            public void onSuccessCallBack(boolean isAttendanceOff) {
                if (isAttendanceOff && !syncOfflineAttendanceRepository.checkIsAttendanceIn()) {
                    AUtils.setIsOnduty(false);
                    ((MyApplication) AUtils.mainApplicationConstant).stopLocationTracking();
                }
            }

            @Override
            public void onFailureCallBack() {

            }
        });
    }



    public void onStartTacking() {


        //Exception thrown when GPS or Network provider were not available on the user's device.
        try {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
            criteria.setAltitudeRequired(false);
            criteria.setSpeedRequired(true);
            criteria.setCostAllowed(false);
            criteria.setBearingRequired(false);

            //API level 9 and up
            criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
            criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);




            assert locationManager != null;
            locationManager.addGpsStatusListener(this);


            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, AUtils.LOCATION_INTERVAL,
                    gpsFreqInDistance, this,null);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, AUtils.LOCATION_INTERVAL,
                    gpsFreqInDistance, this,null);



        } catch (IllegalArgumentException | SecurityException e) {
            Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
            Log.d(TAG, "onStartTacking: " + e.getMessage());
            Log.d(TAG, "onStartTacking: " + e.getLocalizedMessage());

            e.printStackTrace();
        } catch (RuntimeException e) {
            Log.e(TAG, Objects.requireNonNull(e.getLocalizedMessage()));
            Log.d(TAG, "onStartTacking: " + e.getMessage());
            e.printStackTrace();

        }
    }

    public void onStopTracking(Context context) {
        mAdapter.shareLocation();

       // removed initialization from here

        locationManager.removeUpdates(this);
//        cancelAlarm(context, (AlarmManager)context.getSystemService(Context.ALARM_SERVICE));


    }

    /*
     * LOCATION CALLBACKS
     */


    //to get the location change
    @Override
    public void onLocationChanged(Location location) {
        Log.d("okh ", "onLocationChanged:   "+System.currentTimeMillis());


        if (location != null) {

            Log.d(TAG, String.valueOf(location.getAccuracy()));

            if (!AUtils.isNullString(String.valueOf(location.getLatitude())) && !AUtils.isNullString(String.valueOf(location.getLongitude()))) {

                Prefs.putString(AUtils.LAT, String.valueOf(location.getLatitude()));
                Prefs.putString(AUtils.LONG, String.valueOf(location.getLongitude()));

                if (Prefs.getBoolean(AUtils.PREFS.IS_ON_DUTY, false)) {
                    if (updatedTime == 0) {
                        updatedTime = System.currentTimeMillis();
                        Log.d(TAG, "updated Time ==== " + updatedTime);
                    }

                    if ((updatedTime + AUtils.LOCATION_INTERVAL_MINUTES) <= System.currentTimeMillis()) {
                        updatedTime = System.currentTimeMillis();
                        Log.d(TAG, "updated Time ==== " + updatedTime);

                    }

                    mTimer = new Timer();
                    mTimer.schedule(new TimerTaskToGetLocation(), 5,notify_interval);
//                   sendLocation();
                }
            }
        } else {
            Log.d(TAG, "onLocationChanged:  no location found !!");
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d(TAG, "onStatusChanged" + provider + "Status" + status);

    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d(TAG, " onProviderEnabled" + provider);

    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d(TAG, " onProviderDisabled" + provider);
    }


    @Override
    public void onGpsStatusChanged(int event) {

    }



   private void sendLocation() {

        //  mAdapter.shareLocation(getTempList());
        Log.d("okh", "sendLocation: Current Time In Millies "+ System.currentTimeMillis());

        try {
            Calendar CurrentTime = AUtils.getCurrentTime();
            Calendar DutyOffTime = AUtils.getDutyEndTime();

            if (CurrentTime.before(DutyOffTime)) {

                Log.i(TAG, "Before");

                UserLocationPojo userLocationPojo = new UserLocationPojo();

                userLocationPojo.setUserId(Prefs.getString(AUtils.PREFS.USER_ID, ""));
                userLocationPojo.setLat(Prefs.getString(AUtils.LAT, ""));
                userLocationPojo.setLong(Prefs.getString(AUtils.LONG, ""));
                double startLat = Double.parseDouble(Prefs.getString(AUtils.LAT, "0"));
                double startLng = Double.parseDouble(Prefs.getString(AUtils.LONG, "0"));
                userLocationPojo.setDistance(String.valueOf(AUtils.calculateDistance(
                        AUtils.mainApplicationConstant.getApplicationContext(), startLat, startLng)));
//                userLocationPojo.setDatetime(AUtils.getServerDateTime()); //TODO
                userLocationPojo.setDatetime(AUtils.getServerDateTimeLocal());
                userLocationPojo.setOfflineId("0");

                userLocationPojo.setIsOffline(AUtils.isInternetAvailable() && AUtils.isConnectedFast(mContext));

                String UserTypeId = Prefs.getString(AUtils.PREFS.USER_TYPE_ID, AUtils.USER_TYPE.USER_TYPE_GHANTA_GADI);
                if (AUtils.isInternetAvailable()) {
                    TableDataCountPojo.LocationCollectionCount count = syncOfflineRepository.getLocationCollectionCount(AUtils.getLocalDate());
                    if ((UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_GHANTA_GADI) || UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_WASTE_MANAGER))
                            && (count.getLocationCount() > 0 || count.getCollectionCount() > 0)) {
                        syncOfflineRepository.insetUserLocation(userLocationPojo);
                    } else {
                        mUserLocationPojoList.add(userLocationPojo);
                        mAdapter.shareLocation(mUserLocationPojoList);
                        mUserLocationPojoList.clear();
                    }
                } else {
                    if (UserTypeId.equals(AUtils.USER_TYPE.USER_TYPE_EMP_SCANNIFY)) {
                        Type type = new TypeToken<UserLocationPojo>() {
                        }.getType();
                        mLocationRepository.insertUserLocationEntity(new Gson().toJson(userLocationPojo, type));
                    } else {
                        syncOfflineRepository.insetUserLocation(userLocationPojo);
                    }
                    mUserLocationPojoList.clear();
                }

            }
            else {
                Log.i(TAG, "After");

                syncOfflineAttendanceRepository.performCollectionInsert(mContext,
                        syncOfflineAttendanceRepository.checkAttendance(), AUtils.getCurrentDateDutyOffTime());

                AUtils.setIsOnduty(false);
                ((MyApplication) AUtils.mainApplicationConstant).stopLocationTracking();

                Activity activity = ((Activity) AUtils.currentContextConstant);

                if (activity instanceof DashboardActivity) {
                    ((Activity) AUtils.currentContextConstant).recreate();
                    AUtils.DutyOffFromService = true;
                }

                if (!AUtils.isNull(AUtils.currentContextConstant)) {
                    ((Activity) AUtils.currentContextConstant).recreate();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }


    }
  


    private class TimerTaskToGetLocation extends TimerTask {
        @Override
        public void run() {
            
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, AUtils.LOCATION_INTERVAL,
                    gpsFreqInDistance, this,null);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, AUtils.LOCATION_INTERVAL,
                    gpsFreqInDistance, this,null);


        }
    }


}