我似乎无法GoogleApiClient
完成我的项目。我正在使用LocationServices
,OnConnected
即使我client.connect()
在方法中调用了它也不会触发onStart
。我已经搜索了 stackoverflow 并尝试了给出的解决方案,但我仍然无法使其工作。我尝试检查我是否已连接但client.isConnected()
始终输出错误。我的权限是:
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()));
}
}