0

I am Using Fused Location Provider Google API to update location for my app, but it got the same location although I moved device when I got value. Please help me find my wrong?

I'm using the button in ExpandableListAdapter. When user click the button there. My app get location and store in my database. I tried to place LocationCallback any where in the activity however it not change.

My Activity:

public class IndexActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

String UserId;
ExpandableListView mExpandableListView;
List<Customer> info;
List<MetterChangeLog> log;

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private LocationSettingsRequest.Builder builder;
private PendingResult<LocationSettingsResult> result;
private final int REQUEST_LOCATION = 200;
private final int REQUEST_CHECK_SETTINGS = 300;
private Location mLastLocation;

FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);

    try {

        GetMetterChangeLogTask mytt = new GetMetterChangeLogTask(IndexActivity.this);
        mytt.execute();

        log = mytt.get();

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(120000); // two minute interval
    mLocationRequest.setFastestInterval(120000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    /*if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }*/
    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {

                mLastLocation = location;
            }
        };
    };

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addOnConnectionFailedListener(this)
            .addConnectionCallbacks(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onResume() {
    super.onResume();
    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {

                mLastLocation = location;
            }
        };
    };

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                mLocationCallback,
                Looper.getMainLooper());
    }
}

@Override
protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(2000);
    mLocationRequest.setFastestInterval(2000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
    }


}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {

                mLastLocation = location;
            }
        };
    };
}

Then I get mLastLocation to intital ExpandableListView as follow

mExpandableListView = (ExpandableListView) findViewById(R.id.list_customer);
                    ExpandableListAdapter adapter = new ExpandableListAdapter(IndexActivity.this,
                            mExpandableListView, info, SoDoc.substring(0, 1), mLastLocation);

                    mExpandableListView.setItemsCanFocus(true);

                    mExpandableListView.setAdapter(adapter);

In ExpandableListAdapter class, I makes a holder with submit button to get location.

groupHolder.submit.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //button functionalty   ...

            ...
            final Customer index = new Customer();

            index.setLong(mLastlocation.getLongitude());
            index.setLat(mLastlocation.getLatitude());
            ...
}

I tried to get location at 3 places and get the same value. Please help me find my wrong. Sorry my English is not good.

Thanks for support.

0 Answers0