2015-01-12 3 views
2

내 게임의 업적과 리더 보드를 관리하기 위해 ManageGoogleService 클래스를 구현했습니다.activity 클래스 외부에서 onActivityResult를 구현하는 방법

메커니즘을 잠금 해제하고 achivements를 표시하고 리더 보드를 제출하고 표시하면 정상적으로 작동합니다. 하지만 Google에 연결을 해제하거나 리더 보드 설정에서 연결 해제 버튼을 클릭 할 때 문제가 발생합니다 (API 오류를 호출 할 때 로그인하지 않음).

Google 서비스에 다시 연결할 수 없습니다. 문제는 onActivityResult 메서드라고 생각합니다. 어떻게이 클래스를 ManageGoogleService 클래스에 구현할 수 있습니까? 이렇게 할 수있는 방법이 없을 수 있습니다. 이 샘플 레이아웃입니다

public class MainActivity extends Activity implements View.OnClickListener { 


ManageGoogleServices manageGoogleServices; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Button signIn = (Button) findViewById(R.id.button_sign_in); 
    // Button signOut = (Button) findViewById(R.id.button_sign_out); 
    Button one = (Button) findViewById(R.id.one); 
    Button two = (Button) findViewById(R.id.two); 
    Button three = (Button) findViewById(R.id.three); 
    Button four = (Button) findViewById(R.id.four); 
    LinearLayout mainView = (LinearLayout) findViewById(R.id.mainView); 

    manageGoogleServices = new ManageGoogleServices(this,this, mainView); 




    // signIn.setOnClickListener(this); 
    // signOut.setOnClickListener(this); 
    findViewById(R.id.button_sign_in).setOnClickListener(this); 
    findViewById(R.id.button_sign_out).setOnClickListener(this); 

    one.setOnClickListener(this); 
    two.setOnClickListener(this); 
    three.setOnClickListener(this); 
    four.setOnClickListener(this); 
} 


/* protected void onResume(){ 
     int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if(result == ConnectionResult.SUCCESS){ 
      Log.d("MYTAG", "SUCCESS"); 
      GooglePlayServicesUtil.getErrorString(result); 
     }else{ 
      if(result == ConnectionResult.SERVICE_MISSING){ 
       Log.d("MYTAG", "service missing"); 
      } 
      if(result == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){ 
       Log.d("MYTAG", "Service version update required"); 
      } 
      if(result == ConnectionResult.SERVICE_DISABLED){ 
       Log.d("MYTAG", "Service disabled"); 
      } 
      if(result == ConnectionResult.SERVICE_INVALID){ 
       Log.d("MYTAG", "Service invalid"); 
      } 

     } 
    }*/ 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 


// Shows the "sign in" bar (explanation and button). 
private void showSignInBar() { 
    Log.d("MYTAG", "Showing sign in bar"); 
    findViewById(R.id.sign_in_bar).setVisibility(View.VISIBLE); 
    findViewById(R.id.sign_out_bar).setVisibility(View.GONE); 
} 

// Shows the "sign out" bar (explanation and button). 
private void showSignOutBar() { 
    Log.d("MYTAG", "Showing sign out bar"); 
    findViewById(R.id.sign_in_bar).setVisibility(View.GONE); 
    findViewById(R.id.sign_out_bar).setVisibility(View.VISIBLE); 
} 


@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()){ 

     case R.id.button_sign_in: 

      Log.d("MYTAG", "Sign-in button clicked"); 
      manageGoogleServices.connectToGoogleService(); 
      showSignOutBar(); 
      break; 

     case R.id.button_sign_out: 

      // sign out. 
      Log.d("MYTAG", "Sign-out button clicked"); 
      manageGoogleServices.disconnectToGoogleService(); 
      showSignInBar(); 
      break; 

     // unlock the achievement 
     case R.id.one: 


      manageGoogleServices.unlockAchievements(getResources().getString(R.string.achievement_achievement_57)); 

      break; 

     //show the achivements list 
     case R.id.two: 

      manageGoogleServices.showAchievementsList(); 

      break; 

     // submitScore in leaderboards 
     case R.id.three: 


      manageGoogleServices.addScoreToLeaderboard(1,"text",1); 

      break; 

     //see the leaderboards 
     case R.id.four: 

      manageGoogleServices.showLeaderboards(1,"text"); 
      break; 
    } 

} 

:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" style="@style/MainLayout" 
tools:context=".MainActivity"> 

<!-- MAIN DISPLAY --> 
<FrameLayout style="@style/MainDisplay"> 
    <LinearLayout 
     android:id="@+id/mainView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <Button android:id="@+id/one" android:text="@string/click_me" 
      style="@style/Button" /> 
     <Button android:id="@+id/two" android:text="showachievements" 
      style="@style/Button" /> 
     <Button android:id="@+id/three" android:text="updlb" 
      style="@style/Button" /> 
     <Button android:id="@+id/four" android:text="showleaderboards" 
      style="@style/Button" /> 
     </LinearLayout> 


</FrameLayout> 

<!-- SIGN-IN BAR --> 
<LinearLayout android:id="@+id/sign_in_bar" style="@style/SignInOutBar"> 

    <com.google.android.gms.common.SignInButton 
     android:id="@+id/button_sign_in" style="@style/SignInButton" /> 

    <TextView style="@style/SignInOutBarBlurb" android:text="@string/sign_in_explanation" /> 
</LinearLayout> 

<!-- SIGN-OUT BAR --> 
<LinearLayout android:id="@+id/sign_out_bar" style="@style/SignInOutBar" 
    android:visibility="gone"> 

    <TextView style="@style/SignInOutBarBlurb" 
     android:text="@string/you_are_signed_in" /> 

    <Button style="@style/SignOutButton" android:id="@+id/button_sign_out" 
     android:text="@string/sign_out" /> 

</LinearLayout> 

는이 (내가 인터넷에서 읽을 때)

이 내 난 내 코드를 테스트하는 데 사용하는 MainActivity입니다 ManageGoogleServices 클래스입니다.

public class ManageGoogleServices implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 
{ 
    Context context; 
    Activity activityName; 


    GoogleApiClient mGoogleApiClient; 
    int REQUEST_ACHIEVEMENTS = 1; 
    int REQUEST_LEADERBOARD = 1; 

    private static int RC_SIGN_IN = 9001; 

    private boolean mResolvingConnectionFailure = false; 

    private boolean mSignInClicked = false; 

    // Set to true to automatically start the sign in flow when the Activity starts. 
    // Set to false to require the user to click the button in order to sign in. 
    private boolean mAutoStartSignInFlow = true; 



    public ManageGoogleServices(Context context, Activity activityName, View viewForPopups){ 

     this.context = context; 
     this.activityName = activityName; 
     mGoogleApiClient = new GoogleApiClient.Builder(context) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .setViewForPopups(viewForPopups) 
       .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN) 
       .addApi(Games.API).addScope(Games.SCOPE_GAMES) 

       .build(); 

     connectToGoogleService(); 


    } 



    public void connectToGoogleService(){ 

     try { 



      if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()) { 

      /* mGoogleApiClient = new GoogleApiClient.Builder(context) 
         .addConnectionCallbacks(this) 
         .addOnConnectionFailedListener(this) 
         .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN) 
         .addApi(Games.API).addScope(Games.SCOPE_GAMES) 
         .build(); 
      */ 



       mSignInClicked = true; 
       mGoogleApiClient.connect(); 
       Log.d("MYTAG", " GOOGLE API CLIENT CONNECTED"); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    public void disconnectToGoogleService(){ 

     mSignInClicked = false; 

     try { 

      if (mGoogleApiClient.isConnected()) { 

       Games.signOut(mGoogleApiClient); 
       mGoogleApiClient.disconnect(); 
       if (!mGoogleApiClient.isConnected()) { 
        Log.d("MYTAG", "DISCONNESSO CORRETTAMENTE"); 
       } 

      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 


    public void unlockAchievements(String achievementsId){ 

     connectToGoogleService(); 

     if (mGoogleApiClient.isConnected()) { 
      // unlock the achievement. 

      Games.Achievements.unlock(mGoogleApiClient, achievementsId); 



      /* Games.Achievements.unlockImmediate(mGoogleApiClient, achievementsId).setResultCallback(
        new ResultCallback<Achievements.UpdateAchievementResult>() { 

         @Override 
         public void onResult(Achievements.UpdateAchievementResult result) { 


          System.out.println(result.getStatus().getStatusCode()); 
         } 

        }); 
      */ 

     }else{ 
      Log.d("MYTAG", "NOT CONNECTED"); 
     } 



    } 

    public void showAchievementsList(){ 

     connectToGoogleService(); 

     if(mGoogleApiClient.isConnected()){ 

      activityName.startActivityForResult(Games.Achievements.getAchievementsIntent(mGoogleApiClient), REQUEST_ACHIEVEMENTS); 


     }else{ 
      Log.d("MYTAG", "NOT CONNECTED"); 
     } 

    } 

    // update the score to leaderboard 
    public void addScoreToLeaderboard(int gameID , String difficultyLevel, int myScore){ 


     connectToGoogleService(); 
     if(mGoogleApiClient.isConnected()){ 

      switch (gameID) { 

       ... 


      } 

     }else{ 

      Log.d("MYTAG", "NOT CONNECTED"); 

     } 


    } 


    public void showLeaderboards(int gameID , String difficultyLevel){ 

     connectToGoogleService(); 

     if(mGoogleApiClient.isConnected()) { 

      switch (gameID) { 

       ... 
      } 
     }else{ 
      Log.d("MYTAG", "NOT CONNECTED"); 
     } 
    } 






    @Override 
    public void onConnected(Bundle bundle) { 

    } 

    @Override 
    public void onConnectionSuspended(int i) { 

     Log.d("MYTAG", "onConnectionSuspended() called. Trying to reconnect."); 
     connectToGoogleService(); 


    } 



    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 


     if (mResolvingConnectionFailure) { 
      // Already resolving 
      return; 
     } 

     // If the sign in button was clicked or if auto sign-in is enabled, 
     // launch the sign-in flow 
     if (mSignInClicked || mAutoStartSignInFlow) { 
      mAutoStartSignInFlow = false; 
      mSignInClicked = false; 
      mResolvingConnectionFailure = true; 

      // Attempt to resolve the connection failure using BaseGameUtils. 
      // The R.string.signin_other_error value should reference a generic 
      // error string in your strings.xml file, such as "There was 
      // an issue with sign in, please try again later." 
      if (!BaseGameUtils.resolveConnectionFailure(activityName, 
        mGoogleApiClient, connectionResult, 
        RC_SIGN_IN, "sign in!!!!!")) { 
       mResolvingConnectionFailure = false; 
      } 
     } 

    } 

    protected void onActivityResult(int requestCode, int responseCode, Intent intent) { 




     if (requestCode == RC_SIGN_IN) { 
      Log.d("MYTAG", "onActivityResult with requestCode == RC_SIGN_IN, responseCode=" 
        + responseCode + ", intent=" + intent); 
      mSignInClicked = false; 
      mResolvingConnectionFailure = false; 
      if (responseCode == activityName.RESULT_OK) { 
       mGoogleApiClient.connect(); 
      } else { 
       BaseGameUtils.showActivityResultError(activityName, requestCode, responseCode, R.string.signin_other_error); 
      } 
     } 
    } 
} 

누군가 나를 도울 수 있습니까?

답변

1

불행히도, 호출이 있더라도 래퍼 클래스 (ManageGoogleServices)가 아니라 startActivityForResult 메서드를 호출하는 것이므로 불가능합니다.

당신은 활동의 onActivityResult를 방법의 결과를 처리해야하고 거기에서 래퍼 클래스에 결과를 전달하는 :

manageGoogleServices.onResult ("데이터 ....")