我实施了ManageGoogleService课程来管理我的游戏成就和排行榜。
解锁和显示成就的机制,并提交和显示领导者工作正常。 但是当我尝试断开连接谷歌时,或者当我点击排行榜设置中的断开连接按钮时(我在调用API错误时没有登录),我遇到了问题。
我无法重新连接到谷歌服务。 我认为问题是onActivityResult方法。 我如何在ManageGoogleService类中而不是在活动类中实现此方法? 有可能没有办法做到这一点? (正如我在互联网上看到的那样)
这是我用来测试代码的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);
}
}
}
}
有人可以帮助我吗?
答案 0 :(得分:1)
不幸的是,这是不可能的,因为毕竟它是调用startActivityForResult方法的活动,而不是包装类(ManageGoogleServices),即使调用就在那里。
您必须在活动的onActivityResult方法中处理结果,并从那里将结果传递给包装类:
manageGoogleServices.onResult(&#34;数据....&#34)