2

Written a Service class to fetch the location, it is fetching the location on some devices like Samsung s7 edge while failed to fetch location on devices like Samsung j5. I just want to know whether it is code issue or mobile specific issue. The code below is the Service class to fetch Location:

    public class LocationService extends Service {
    private static final String TAG = "myService";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 1000;
    private static final float LOCATION_DISTANCE = 50f;
    boolean inside;
    TelephonyManager tMgr;
    String mPhoneNumber;

    private class MyLocationListener implements 
    android.location.LocationListener {
        Location mLastLocation;

        public MyLocationListener(String provider) {
            Log.d(TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        }

        @Override
        public void onLocationChanged(Location location) {
            Log.d("myLocation", "onLocationChanged: " + location);
            Location locationA = new Location("point A");
            locationA.setLatitude(33.65340376);
            locationA.setLongitude(73.03382854);
            float distance = locationA.distanceTo(location);

            Log.e(TAG, "Total distance: " + distance);
            mLastLocation.set(location);
        }

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

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

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

    MyLocationListener[] mLocationListeners = new MyLocationListener[]{
            new MyLocationListener(LocationManager.GPS_PROVIDER),
            new MyLocationListener(LocationManager.NETWORK_PROVIDER)
    };

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        int lastStatus = db.getLastTimeStatus();
        if (lastStatus == 0) {
            inside = true;
        } else if (lastStatus == 1) {
            inside = false;
        }
        tMgr = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        mPhoneNumber = tMgr.getLine1Number();

        Log.e(TAG, "onCreate");
        initializeLocationManager();

        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[0]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }
    }

    @Override
    public void onDestroy()
    {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        if (mLocationManager != null) {
            for (int i = 0; i < mLocationListeners.length; i++) {
                try {
                    mLocationManager.removeUpdates(mLocationListeners[i]);
                } catch (Exception ex) {
                    Log.i(TAG, "fail to remove location listners, ignore", ex);
                }
            }
        }
    }

    private void initializeLocationManager() {
        Log.e(TAG, "initializeLocationManager");
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }
    }

Every other method like onProviderEnabled() etc works fine but onlocationChanged() is not picking user's current location most of the time, but sometime it suddenly fires 4, 5 location at the same time.

Aju
  • 4,597
  • 7
  • 35
  • 58
fazal nabi
  • 39
  • 1
  • 4

0 Answers0