android应用关闭后,如何保持我的google plus会话打开? [英] How can I keep my google plus session opened after android application close?

查看:60
本文介绍了android应用关闭后,如何保持我的google plus会话打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在这里发布信息,因为我以前从未有过这样的需要,因为我已经回答了每个问题!

this is my first time posting here because I've never had the need before because every question I had was already answered!

问题是我正在尝试使用google plus登录我的android应用程序,但是如果我关闭了我的应用程序,我不知道如何查看用户是否已经登录.会检查吗?

The thing is that I'm trying to log in my android application with google plus but if I close my application.. I don't know how to see if the user was already signed in.. Is there any way to do check it?

例如: -您登录我的应用程序,然后转到MainActivity而不是登录活动. -然后您不注销,只需关闭我的应用程序一个小时即可. -之后..您再次打开我的应用程序,然后再次进入MainActivity ..您再次处于登录活动中..

For example: - You login in my application and then you go to the MainActivity instead of the login activity. - Then you don't log out, you simply close my app for.. maybe half an hour.. - After that.. you open my app again and instead go to the MainActivity again.. you are in the login activity again..

有没有办法知道您是否已经登录?

Is there any way to know if you were already signed in?

这是我的登录课程:

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

非常感谢您!

推荐答案

好的,我会回答自己.

由于我仍然不知道是否可以自动执行此操作,因此我现在通过在onConnected方法中保存一个共享的首选项来做到这一点:

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

然后我以断开连接的方式将其删除 //Google注销

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

然后,如果我的偏好设置为true,我将检查onCreateView:

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

这篇关于android应用关闭后,如何保持我的google plus会话打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆