【问题标题】:Can't connect to GoogleAPIClient LocationServices.API无法连接到 GoogleAPIClient LocationServices.API
【发布时间】:2016-05-16 00:15:37
【问题描述】:

我似乎无法让GoogleApiClient 参与我的项目。我正在使用LocationServices,即使我在onStart 方法中调用了client.connect()OnConnected 也没有触发。 我已经搜索了 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()));
}

}

【问题讨论】:

    标签: android geolocation location-services android-googleapiclient


    【解决方案1】:

    试试这个!!

    首先:删除 buildGoogleApiClient();在 Oncreate();

    然后:

    public static final int REQUEST_CODE_RESOLUTION = 1;
    
    @Override
    protected void onResume() {
        super.onResume(); 
      buildGoogleClient();
    }
    
    private void buildGoogleClient(){
     if(client != null ){
     client = new GoogleApiClient.Builder(this)
     .addApi(LocationServices.API)
     .addConnectionCallbacks(this)
     .addOnConnectionFailedListener(this).build();
     }
     client.connect();
    }
    
      @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
        if (!result.hasResolution()) {
            // show the localized error dialog.
            GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
            return;
        }
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }
    }
    
        @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
            client.connect();
    
         // Enter code get Latude and Longtude
    
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(client);
     longt = mLastLocation.getLongitude();
     latt = mLastLocation.getLatitude();
     if (mLastLocation != null) {
      mLatitudeText.setText(mLastLocation.getLatitude()+"");
      mLongitudeText.setText(mLastLocation.getLongitude()+"");
     }
        }
     }
    

    【讨论】:

    • 它有效,但我又遇到了一个错误。不确定这是否仍然相关。 GoogleApiClient is not configured to use the API required for this call.
    • 可以在androidhive.info/2015/02/…看谷歌客户端定位教程
    猜你喜欢
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    相关资源
    最近更新 更多