How can I keep my google plus session opened after android application close?

一世执手 提交于 2019-12-06 09:01:01

Ok, I will answer myself.

As I still don't know if there is a way to do it automatically, I do it at the moment by saving a shared preference in the onConnected method:

@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();
}

And i delete it in my disconnect method // Google log out

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();

And then, I check in the onCreateView if my preference is true:

@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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!