Я реализовал класс ManageGoogleService для управления достижениями и рейтингами моей игры.Как реализовать onActivityResult за пределами класса активности
Механизм разблокировки и достижения достижений, а также представление и показ лидеров-лидеров отлично работают. Но у меня возникают проблемы, когда я пытаюсь отключиться от Google, или когда я нажимаю кнопку разъединения в настройках лидеров (при вызове API не был подписан).
Я не могу подключиться к сервису google. Я думаю, что проблема заключается в методе 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);
}
}
}
}
Может кто-нибудь мне помочь?