1

내 상황 :GoogleApiClient 및 GooglePlayServicesClient : 관심사를 분리하여 보존 할 수 있습니까?

  1. 먼저 앱에서 Google Plus 인증을 구현했습니다. 나는 quick start instructions을 따라 가서 quick start sample app code을 내 앱에 추가했습니다.
  2. 그런 다음 사용자의 마지막으로 알려진 위치를 얻고 싶었습니다. 융합 위치 제공자는 그것을 얻는 가장 현대적인 방법 인 것처럼 보였으므로 나는 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 
    

그리고 코드 모두 네임 스페이스는 다음과 같은 우선합니다 m : 당신이이 GoogleApiClient 또는 onConnectedonConnectionFailed 이벤트 핸들러를 트리거 LocationClient 인 경우 식별하는 코드를 작성하도록 강요 될 것이다

@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 
      } 
      */ 
} 

이러한 것을.


내 질문 :

  • 나는 separation of concerns을 유지하고 싶습니다. 이것에 대해 갈 수있는 더 좋은 방법이 있습니까?
+0

와 LocationClient을 만들

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

와 GoogleApiClient을 만들 http://stackoverflow.com/questions/31734567/separating-the에서보세요 -concerns-of-activity-and-googleapiclient/31734568 # 31734568 문제의 완전한 해결책은 아니지만 몇 가지 아이디어를 줄 수 있습니다. –

답변

4

활동 클래스 자체에 이러한 인터페이스를 구현하는 대신 활동 클래스에 별도의 오브젝트 구성원을 작성할 수 있습니다.

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) { 
     } 
    }; 
    ... 
} 

그리고,

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