如何在activity类之外实现onActivityResult

问题描述 投票:0回答:2

我实现了 ManageGoogleService 类来管理我的游戏成就和排行榜。

解锁和展示成就、提交和展示排行榜的机制工作正常。 但是当我尝试断开与谷歌的连接时,或者当我单击排行榜设置中的断开连接按钮时(调用 API 错误时未登录),我遇到了问题。

我无法重新连接到 Google 服务。 我认为问题出在 onActivityResult 方法上。 我如何在我的 ManageGoogleService 类中而不是在 Activity 类中实现此方法? 有没有可能没有办法做到这一点? (我在网上读到的)

这是我用来测试我的代码的 MainActivity:

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>

这是我的 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);
            }
        }
    }
}

有人可以帮助我吗?

android google-play-services onactivityresult
2个回答
2
投票

不幸的是,这是不可能做到的,因为毕竟是 Activity 调用了 startActivityForResult 方法,而不是包装类 (ManageGoogleServices),即使调用在那里。

您必须在活动的 onActivityResult 方法中处理结果,并从那里将结果传递给包装类:

manageGoogleServices.onResult(“数据....”)


0
投票

2022更新

是的,如果您的项目使用 Android 10,这是可能的。

如果您有 Activity 实例且其类型为 AppCompatActivity,您将拥有 Activity.registerForActivityResult(),如下所示:

(activity as? AppCompatActivity)?.apply {
//-------------------------------------------------------<InputType, OutputType>
    registerForActivityResult(object : ActivityResultContract<Unit, String?>() {
        override fun createIntent(context: Context, input: Unit?): Intent =
            Intent(YOUTCLASS:class.java).apply {
            // Put your extras here
            }

        override fun parseResult(resultCode: Int, intent: Intent?) =
           // GetYour extras here
           intent?.getStringExtra(ShortcutsNotLoggedActivity.REDIRECT_OPKEY)

    }) { resultStringInThisCase ->
        // Do whatever you want with the result
    }.launch(null) // Pass your input if you has one
}

这样,您就可以注册其之外的任何活动的结果。

Android 开发者文档上的更多信息和 Java 版本: https://developer.android.com/training/basics/intents/result#kotlin

© www.soinside.com 2019 - 2024. All rights reserved.