I am currently developing an app together with a friend of mine and I'm using the following code to acquire my current location:
public class AdressPopup extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
public static final String TAG = AdressPopup.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
TextView textViewCord;
Button okayButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adress_finder);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
// GUI items:
textViewCord = (TextView) findViewById(R.id.textViewCord);
okayButton = (Button) findViewById(R.id.button);
// Terminate window upon clicking 'okayButton':
okayButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
// How much to cover of the window..
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .6), (int) (height * .35));
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
@Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
Locale svLocale = new Locale("sv", "SE");
//Get address based on location:
try {
Geocoder geo = new Geocoder(AdressPopup.this.getApplicationContext(), svLocale.getDefault());
List<android.location.Address> addresses;
addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.isEmpty()) {
textViewCord.setText("Waiting for Location");
} else {
if (addresses.size() > 0) {
final String postalCode = addresses.get(0).getPostalCode();
final String streetName = addresses.get(0).getThoroughfare();
// final String cityName = addresses.get(0).getLocality();
StoreBackend.lookupStoreName(postalCode, new StoreBackend.Callback<String>() {
@Override
public void acceptResult(String name) {
textViewCord.append(postalCode + " (" + streetName + ") " + "\n\n" + "Din butik" + ": " + name);
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Log.d(TAG, "Location services connected.");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//do nothing
} else if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
handleNewLocation(location);
}
// Log.d("Coordinates: ", location.getLatitude() + "," + location.getLongitude());
onLocationChanged(location);
}
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Location services suspended. Please reconnect.");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
}
We are using android studio together with bitbucket (where our repository for this project is located). This works perfectly well when I export the .apk file to my samsung galaxy s4 device. However, when my friend does the same from his android studio environment and tries this functionality on his samsung galaxy s7 (mind you, we are working with identical code) it's not working.
He is getting a null value when requesting a new location on the following line:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
These permissions are present in the AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
and the build that is being used for the play-service is '6.5.87'. Any help on the matter would be greatly appreciated.