android - GoogleApiClient 和 GooglePlayServicesClient : Can you preserve a separation of concerns?

标签 android google-play-services separation-of-concerns google-api-client location-client

我的情况:

  1. 首先,我在应用中实现了 Google Plus 身份验证。我关注了quick start instructions并添加了 quick start sample app code到我的应用程序。
  2. 然后我想获取用户最后的已知位置。 Fused Location Provider 似乎是获取它的最现代的方式,所以我查看了 LocationUpdates.zipkpbird's demo code .

我的担忧:

  • com.google.android.gms.common.api.GoogleApiClientcom.google.android.gms.common.GooglePlayServicesClient 命名空间引入了一些重叠事实上,如果您想使用 GoogleApiClientLocationClient,那么您的类(即 Activity)必须实现以下内容:

    GoogleApiClient.ConnectionCallbacks,  
    GoogleApiClient.OnConnectionFailedListener,  
    GooglePlayServicesClient.ConnectionCallbacks,  
    GooglePlayServicesClient.OnConnectionFailedListener
    

来自两个命名空间的代码将覆盖以下内容:

@Override
public void onConnected(Bundle connectionHint) {
            /* pseudo-code
            if (GoogleApiClient) {
               // Implementation
            } else {                
               // Must be LocationClient
            }
            */
}

@Override
public void onConnectionFailed(ConnectionResult result) {
            /* pseudo-code
            if (GoogleApiClient) {
               // Implementation
            } else {                
               // Must be LocationClient
            }
            */
}

这样您将被迫编写代码来辨别是 GoogleApiClient 还是 LocationClient 触发了 onConnectedonConnectionFailed 事件处理程序。


我的问题:

最佳答案

您可以在 Activity 类中创建单独的对象成员,而不是在 Activity 类本身中实现这些接口(interface)。

public YourActivity extends Activity {
    ...
    private GoogleApiClient.ConnectionCallbacks apiClient1 = new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnectionSuspended(int cause) {
        }

        @Override
        public void onConnected(Bundle connectionHint) {
        }
    };

    private GoogleApiClient.OnConnectionFailedListener apiClient2 = new GoogleApiClient.OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(ConnectionResult result) {
        }
    };

    private GooglePlayServicesClient.ConnectionCallbacks servicesClient1 = new GooglePlayServicesClient.ConnectionCallbacks() {
        @Override
        public void onDisconnected() {
        }

        @Override
        public void onConnected(Bundle connectionHint) {
        }
    };

    private GooglePlayServicesClient.OnConnectionFailedListener servicesClient2 = new GooglePlayServicesClient.OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(ConnectionResult result) {
        }
    };
    ...
}

然后,创建您的 GoogleApiClient

GoogleApiClient.Builder builder = new GoogleApiClient.Builder(yourContext, apiClient1, apiClient2);
builder.addApi(...).addScope(...);
GoogleApiClient apiClient = builder.build();

使用以下命令创建您的 LocationClient

LocationClient locClient = new LocationClient(yourContent, servicesClient1, servicesClient2);

关于android - GoogleApiClient 和 GooglePlayServicesClient : Can you preserve a separation of concerns?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22733661/

相关文章:

android - 如何在android中显示如图所示的自定义错误对话框?

android - 如何将 android T.V 模拟器连接到互联网?

android - 使用 MediaStore 在 Android Q 中创建/复制文件

android - 发布到 G+ 用户流时的回调

django - 对 Django + AngularJS 现实世界示例进行分类

android - 远程服务调用 PeripheralManager.getInstance

android - NoClassDefFoundError : com. google.android.gms.analytics.Tracker

java - 如何仅在用户登录时执行此代码

user-interface - 将接口(interface)与逻辑分离 - Java

ruby-on-rails-3 - 范围是在 Rails 类还是实例方法中?