我目前正在和我的一个朋友一起开发一个应用程序,我正在使用以下代码来获取我当前的位置:
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());
}
}
}
我们正在使用 android studio 和 bitbucket(我们的项目存储库所在的位置)。当我将 .apk 文件导出到我的三星 Galaxy s4 设备时,这非常有效。但是,当我的朋友在他的 android studio 环境中执行相同操作并在他的 samsung Galaxy s7 上尝试此功能时(请注意,我们正在使用相同的代码)它不起作用。
在以下行请求新位置时,他得到一个空值:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
这些权限存在于 AndroidManifest.xml 文件中:
<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" />
并且用于播放服务的构建是“6.5.87”。任何有关此事的帮助将不胜感激。