【问题标题】:GoogleApiClient OnConnected Not Called unless Service is stopped除非服务停止,否则不会调用 GoogleApiClient OnConnected
【发布时间】:2017-04-14 15:14:31
【问题描述】:

我正在调用一个连接到 googleapiclient 的服务。每当第一次调用服务时,一切正常,客户端已连接,Onconnected 被调用。但是,当我不停地回忆服务时,我连接了 googleapiclient,但从未调用过 Onconnected。为什么这样?是否每次都需要终止服务。代码如下:

public class ALW extends Service implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks, LocationListener {
    private GoogleApiClient mGoogleApiClient;



    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        Log.d("ALWFA", "Stopped");
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("ALWFA", "Called");
        if (!mGoogleApiClient.isConnected()) {
            Log.e("ALWFA", "Called for Connection");
            mGoogleApiClient.connect();
        } else {
            Log.e("ALWFA", "Already Connected");
        }
        //Do Work
        return super.onStartCommand(intent, flags, startId);
    }

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


    @Override
    public void onCreate() {
        super.onCreate();
        buildGoogleApiClient();
    }


    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d("Google Client", "Connected");
        //Do work

    }

    @Override
    public void onConnectionSuspended(int i) {
        System.out.println("Connection Sus");
        buildGoogleApiClient();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.i("Connection Error", "onConnectionFailed:" + connectionResult.getErrorCode() + "," + connectionResult.getErrorMessage());
        buildGoogleApiClient();
        System.out.println("Connection Failed");
    }

    synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }


}

而且我在 Onconenctifailed 中没有收到任何错误。通常代码停留在 onStartCommand:“ALWFA Called”。每当第二次调用服务时。

【问题讨论】:

  • 这就是我所做的,我称之为连接。 Log.d("ALWFA", "调用"); if (!mGoogleApiClient.isConnected()) { Log.e("ALWFA", "Called for Connection"); mGoogleApiClient.connect();

标签: java android google-api-client


【解决方案1】:

你应该在像这样再次调用它之前检查一下

   if(mGoogleApiClient!=null && !mGoogleApiClient.isConnected())
      {
        //Do your work
      }

并实现这些方法

  @Override
protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null)
        mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient != null)
        mGoogleApiClient.disconnect();

}

【讨论】:

  • 我在 onstart 中检查了 null 和 isconnected 并且两者都为真,并且代码再次没有调用 Onconnected。
  • 您不应在开始调用 mGoogleApiClient.connect() 时检查 isConnected;开始
  • 它正在工作,只是之前的一个查询我直接调用了 connect 方法,这正在创建问题,如何检查 !null 在这里有什么不同?现在 !null 实现了,它再次被称为 connect 方法。
  • 首先,您应该始终检查 mGoogleclient 对象是否已实例化,我们检查 !null 并且如果已实例化,则只有您可以使用 googleapiclient
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多