简体   繁体   English

连接到Google Api客户端时连接失败

[英]Connecting fail when connect to the Google Api Client

I try connecting to the Google API Client to achieve some functionality in my application. 我尝试连接到Google API客户端以实现我的应用程序中的某些功能。 But for some reason I cannot connect to the Google API Client And because that I can not continue to use Google services. 但是由于某种原因,我无法连接到Google API客户端,并且由于该原因,我无法继续使用Google服务。

After I initialize the GoogleApiClient I check if mGoogleApiClient.isConnected and always get it False and I don't know why. 初始化GoogleApiClient后,我检查mGoogleApiClient.isConnected是否始终为False,我不知道为什么。 I would appreciate any help. 我将不胜感激任何帮助。 Thanks. 谢谢。

Notice that everything happens on background service. 注意,所有事情都在后台服务上发生。

public class Background extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {

private String TAG = "Background";

private GoogleApiClient mGoogleApiClient;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    Log.e(TAG,"Start");


    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(ActivityRecognition.API)
            .addApi(LocationServices.API)
            .build();

    if (!mGoogleApiClient.isConnected()) {
        Log.e(TAG,"GoogleApiClient not yet connected");
    } else {
        Log.e(TAG,"GoogleApiClient connected");

    }

    return Service.START_STICKY;
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.e(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());

}

@Override
public void onResult(Status status) {
    if (status.isSuccess()) {
        Log.e(TAG, "Successfully added activity detection.");

    } else {
        Log.e(TAG, "Error: " + status.getStatusMessage());
    }
}

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "Connected");
    mGoogleApiClient.connect();
}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "Connection suspended");
    mGoogleApiClient.connect();
}

you are not calling .connect() in the right place. 您没有在正确的位置调用.connect()。 You are building a client, and you must call .connect() after that. 您正在建立一个客户端,然后必须调用.connect()。 You are immediately checking if it is connected, which it will never be. 您将立即检查它是否已连接(永远不会连接)。

Please add .connect() after .build() in .onStartCommand() and remove this code: 请在.onStartCommand()中的.build()之后添加.connect()并删除以下代码:

if (!mGoogleApiClient.isConnected()) {
    Log.e(TAG,"GoogleApiClient not yet connected");
} else {
    Log.e(TAG,"GoogleApiClient connected");
}

Also, remove this since it might get you in an unwanted state: 另外,请删除此内容,因为它可能会使您陷入不想要的状态:

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "Connected");
    // remove this -> mGoogleApiClient.connect();
}

Connect the googleApiClient in onStartCommand of your service 连接googleApiClient在onStartCommand您服务

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


Log.e(TAG,"Start");


mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(ActivityRecognition.API)
        .addApi(LocationServices.API)
        .build();

  if (!mGoogleApiClient.isConnected()) {
    Log.e(TAG,"GoogleApiClient not yet connected");

    mGoogleApiClient.connect(); // connect it here..

  } else {
    Log.e(TAG,"GoogleApiClient connected");

}

return Service.START_STICKY;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM