gpt4 book ai didi

android - 如何在 Activity 类之外实现 onActivityResult

转载 作者:行者123 更新时间:2023-12-02 01:42:11 33 4
gpt4 key购买 nike

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

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

我无法重新连接到谷歌服务。我认为问题出在 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);
}
}
}
}

有人可以帮助我吗?

最佳答案

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

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

manageGoogleServices.onResult("数据....")

关于android - 如何在 Activity 类之外实现 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27900662/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com