0

I'm new to JAVA and ANDROID and tried to build an app to have the location as per this, so I wrote the below helper code:

package oryx.tecna.locateme;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;

public class UtilLocation
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    protected static GoogleApiClient mGoogleApiClient;
    protected static Location mLastLocation;

    public UtilLocation(Context context) {
        mGoogleApiClient = new GoogleApiClient.Builder(Oryx.getContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(Oryx.getContext(),
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(Oryx.getContext(),
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation == null)
            Toast.makeText(Oryx.getContext(), R.string.no_location_detected, Toast.LENGTH_LONG).show();
        else
            Toast.makeText(Oryx.getContext(), "Location found", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(Oryx.getContext(), "Connection suspended", Toast.LENGTH_SHORT).show();
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Toast.makeText(Oryx.getContext(), "Connection failed: ConnectionResult.getErrorCode() = "
                + connectionResult.getErrorCode(), Toast.LENGTH_SHORT).show();
    }
}

I created the below to to get the context:

package oryx.tecna.locateme;

import android.app.Application;
import android.content.Context;

public class Oryx extends Application {

    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
    }

    public static Context getContext() {
        return mContext;
    }
}

Then I tried to call the above helper using the below code:

location = (new UtilLocation(context)).mLastLocation;

but the app had been crashed :(

What is the mistake I did and how to get it fixed? thanks

UPDATE I found one error, so i replaced the:

mGoogleApiClient = new GoogleApiClient.Builder(Oryx.getContext())

by:

mGoogleApiClient = new GoogleApiClient.Builder(context)

The app now is not crashing, but not returning the location, i.e. the mLastLocation is read as `nullذ

Update 2

Apparantly,

public void onConnected(@Nullable Bundle bundle)

is not executed as none of the Toast appears, neither the one telling there is GPS, nor the one telling there is no GPS

Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
  • Have you added permissions to the manifest? – MichaelStoddart Mar 21 '17 at 12:27
  • yes @MichaelStoddart, apparently the `public void onConnected(@Nullable Bundle bundle)` is not executed as I none of the `Toast` appears, neither the one telling there is GPS, nor the one telling there is no GPS – Hasan A Yousef Mar 21 '17 at 12:52
  • "The app now is not crashing, but not returning the location, i.e. the mLastLocation is read as null". That means you haven't yet received any coordinate. As soon as you receive one coordinate, after that `getLastLocation()` will return non-null value. – azizbekian Mar 21 '17 at 13:17
  • @azizbekian I understand that, , apparently the public void onConnected(@Nullable Bundle bundle) is not executed as I none of the Toast appears, neither the one telling there is GPS, nor the one telling there is no GPS – Hasan A Yousef Mar 21 '17 at 13:34

0 Answers0