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