1
我似乎無法使GoogleApiClient
在我的項目上工作。我正在使用LocationServices
,OnConnected
即使我在onStart
方法中調用client.connect()
也不會觸發。 我搜索了stackoverflow並嘗試瞭解決方案,但我仍然無法使其工作。 我試着檢查我是否連接,但client.isConnected()
總是輸出false。 我的權限是:無法連接到GoogleAPIClient LocationServices.API
android.permission.INTERNET android.permission.ACCESS_COARSE_LOCATION android.permission.ACCESS_FINE_LOCATION
我失去了一些東西?
編輯:
private GoogleApiClient client;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ButterKnife.inject(this);
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient() {
client = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
@Override
public void onStart() {
// client.connect();
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Log.e("Connected?", String.valueOf(client.isConnected()));
}
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e("Failed?", connectionResult.getErrorMessage());
}
@Override
public void onConnected(Bundle bundle) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
client);
longt = mLastLocation.getLongitude();
latt = mLastLocation.getLatitude();
if (mLastLocation != null) {
// mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
// mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
它的工作,但我得到了另一個錯誤。不知道這是否仍然相關。 'GoogleApiClient未配置爲使用此調用所需的API。' – Redan
您可以在http://www.androidhive.info/2015/02/android-location-api-using-google-play上看到Google客戶端位置教程-服務/ –