2015-04-17 26 views
2

這是我第一次在這裏發帖,因爲我從來沒有過的需求,因爲每一個問題,我有已經回答了!如何在Android應用程序關閉後打開我的Google plus會話?

的事情是,我試圖登錄我與谷歌Android應用程序加號,但如果我關閉我的應用程序..我不知道怎麼看,如果用戶在已經簽署了..有什麼辦法做檢查嗎?

例如: - 你登錄我的應用程序,然後你去在MainActivity而不是登錄活動。 - 那你不註銷,你只需關閉我的應用程序的..也許半小時.. - 在那之後..你再次打開我的應用程序,而是去在MainActivity再次..你在登錄活動再次..

有沒有辦法知道,如果你已經登錄?

這是我的登錄類:

import android.content.Intent; 
import android.content.IntentSender; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.SignInButton; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.Scope; 
import com.google.android.gms.plus.Plus; 

public final class LoginGPlusFragment extends Fragment implements 
     View.OnClickListener, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener 
{ 
    /* Request code used to invoke sign in user interactions. */ 
    private static final int RC_SIGN_IN = 0; 

    /** 
    * True if we are in the process of resolving a ConnectionResult 
    */ 
    private boolean mIntentInProgress; 

    /** 
    * True if the sign-in button was clicked. When true, we know to resolve all 
    * issues preventing sign-in without waiting. 
    */ 
    private boolean mSignInClicked; 

    static GoogleApiClient mGoogleApiClient; 
    SignInButton btnLogin; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     return inflater.inflate(R.layout.fragment_gplus_login, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) 
    { 
     Log.d("DEBUG","onViewCreated LoginGPlusFragment"); 
     super.onViewCreated(view, savedInstanceState); 
     if(mGoogleApiClient == null || !mGoogleApiClient.isConnected()) 
      mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(Plus.API) 
        .addScope(new Scope("profile")) 
        .build(); 
     else 
      Log.d("DEBUG","onViewCreated you're already connected"); 
     btnLogin = (SignInButton)view.findViewById(R.id.sign_in_button); 
     btnLogin.setOnClickListener(this); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) 
    { 
     Log.d("DEBUG","onConnectionFailed LoginGPlusFragment"); 
     if (!mIntentInProgress) 
     { 
      if (mSignInClicked && result.hasResolution()) 
      { 
       // The user has already clicked 'sign-in' so we attempt to resolve all 
       // errors until the user is signed in, or they cancel. 
       try 
       { 
        result.startResolutionForResult(getActivity(), RC_SIGN_IN); 
        mIntentInProgress = true; 
       } catch (IntentSender.SendIntentException e) { 
        // The intent was canceled before it was sent. Return to the default 
        // state and attempt to connect to get an updated ConnectionResult. 
        mIntentInProgress = false; 
        mGoogleApiClient.connect(); 
       } 
      } 
     } 
    } 

    @Override 
    public void onClick(View view) 
    { 
     if (view.getId() == R.id.sign_in_button && !mGoogleApiClient.isConnecting()) 
     { 
      mSignInClicked = true; 
      mGoogleApiClient.connect(); 
     } 
    } 

    @Override 
    public void onResume() 
    { 
     super.onResume(); 
     Log.d("DEBUG","onResume LoginGPlusFragment"); 
     if(mGoogleApiClient!=null && mGoogleApiClient.isConnected()) 
      launchChatActivity(); 
     else 
      Log.d("DEBUG","onResume you are disconnected"); 
    } 

    @Override 
    public void onConnected(Bundle bundle) 
    { 
     Log.d("DEBUG","onConnected LoginGPlusFragment"); 
     mSignInClicked = false; 
     launchChatActivity(); 
    } 


    private void launchChatActivity() 
    { 
     String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient); 
     Log.d("DEBUG", "Connected with google. You are "+accountName); 
     btnLogin.setVisibility(View.INVISIBLE); 
     Intent i = new Intent(getActivity(), ChatActivity.class); 
     startActivity(i); 
     getActivity().finish(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) 
    { 
     Log.d("DEBUG","onConnectionSuspended LoginGPlusFragment"); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int responseCode, Intent data) 
    { 
     super.onActivityResult(requestCode, responseCode, data); 
     Log.d("DEBUG","onActivityResult LoginGPlusFragment"); 

     if (requestCode == RC_SIGN_IN) 
     { 
      if (responseCode != getActivity().RESULT_OK) 
      { 
       mSignInClicked = false; 
      } 

      mIntentInProgress = false; 

      if (!mGoogleApiClient.isConnected()) 
      { 
       mGoogleApiClient.reconnect(); 
      } 
     } 
    } 
} 

非常感謝您!

回答

3

好吧,我會回答自己。

正如我仍然不知道是否有一種方法可以自動做到這一點,我這樣做是目前通過保存在onConnected方法共享偏好:

@Override 
public void onConnected(Bundle bundle) 
{ 
    SharedPreferences sharedPref = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPref.edit(); 
    editor.putBoolean("signed_in_with_google", true); 
    editor.commit(); 
    Log.d("DEBUG","onConnected LoginGPlusFragment"); 
    mSignInClicked = false; 
    launchChatActivity(); 
} 

和我刪除我的斷開方法 //谷歌退出

if(LoginGPlusFragment.mGoogleApiClient.isConnected()) 
     LoginGPlusFragment.mGoogleApiClient.disconnect(); 

SharedPreferences sharedPref = getSharedPreferences("login", Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPref.edit(); 
editor.putBoolean("signed_in_with_google", false); 
editor.commit(); 
returnToLoginScreen(); 

然後,我在onCreateView檢查我的選擇是正確的:

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) 
{ 
    Log.d("DEBUG","onViewCreated LoginGPlusFragment"); 
    super.onViewCreated(view, savedInstanceState); 
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(Plus.API) 
      .addScope(new Scope("profile")) 
      .build(); 
    SharedPreferences pref = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE); 
    boolean signed = pref.getBoolean("signed_in_with_google", false); 

    btnLogin = (SignInButton) view.findViewById(R.id.sign_in_button); 
    btnLogin.setOnClickListener(this); 

    if(signed) 
    { 
     Log.d("DEBUG","You were previously signed in with google."); 
     connect(); 
    } 
} 
相關問題