0
我正在关注Google的教程,关于如何使用GoogleApiClient
获取我的Android应用中的位置,但是我在构建客户端并尝试连接后遇到了问题。我看到客户端正在尝试连接,但onConnected()
和onConnectionFailed()
回调都不会被调用。因此,客户永远不会建立连接或报告他未能这样做。我在这里错过了什么?Android的GoogleApiClient的onConnect()回调永远不会被调用
更新:添加了访问位置的权限检查。
public class MainActivity extends AppCompatActivity
implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
public static final String LOG_TAG = MainActivity.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private final static int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 0;
private Location mCurrentLocation = new Location("default");
private GoogleApiClient mGoogleApiClient = null;
private Location mLastLocation = null;
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Log.e("Connected: ", String.valueOf(mGoogleApiClient.isConnected()));
mGoogleApiClient.connect();
Log.e("Connected: ", String.valueOf(mGoogleApiClient.isConnected()));
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// check if permissions are granted
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
@Override
protected void onStop() {
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
super.onStop();
}
private void handleNewLocation(Location location) {
Log.d(LOG_TAG, location.toString());
}
@Override
public void onConnected(@Nullable Bundle bundle) {
try {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation == null) {
}
else {
handleNewLocation(mLastLocation);
}
} catch (SecurityException e) {
DialogFragment mlocationDialog = new LocationDialogFragment();
mlocationDialog.show(getSupportFragmentManager(), "locationDialog");
}
}
@Override
public void onConnectionFailed(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(LOG_TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
是的,我没有尝试,但发现该权限已发,所以这不是问题。 – martasd
手动管理连接时,您需要在应用生命周期的正确位置调用connect()和disconnect()方法。在活动上下文中,最佳做法是在您的活动的onStart()方法中调用connect(),并在您的活动的onStop()方法中调用disconnect()。当使用自动管理连接时,会自动调用connect()和disconnect()方法。 –