在片段的Andr​​oid谷歌+登录 [英] Android google+ login in a fragment

查看:117
本文介绍了在片段的Andr​​oid谷歌+登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用的片段,所以我可以从不同的活动,用它来实现谷歌+登录。我创建的片段像这样

Currently I am trying to implement google+ login using fragment so I can use it from different activities. I have created fragment like this

public class GoogleSignUpFragment extends Fragment implements
    ConnectionCallbacks, OnConnectionFailedListener, OnClickListener {

// PlusClient Variables
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private ProgressDialog mConnectionProgressDialog;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    mPlusClient = new PlusClient.Builder(getActivity()
            .getApplicationContext(), this, this)
            .setActions("http://schemas.google.com/AddActivity",
                    "http://schemas.google.com/BuyActivity")
            .setScopes(Scopes.PLUS_LOGIN, Scopes.PLUS_PROFILE).build();

    getActivity().findViewById(R.id.sign_in_button)
            .setOnClickListener(this);

    // Progress bar to be displayed if the connection failure is not
    // resolved.
    mConnectionProgressDialog = new ProgressDialog(getActivity());
    mConnectionProgressDialog.setMessage("Signing in...");

}

@Override
public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    mPlusClient.connect();
}

@Override
public void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    mPlusClient.disconnect();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR
            && resultCode == Activity.RESULT_OK) {
        mConnectionResult = null;
        mPlusClient.connect();
    }
}

@Override
public void onClick(View view) {
    if (view.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
        if (mConnectionResult == null) {
            mConnectionProgressDialog.show();

        } else {
            try {
                mConnectionResult.startResolutionForResult(getActivity(),
                        REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                // Try connecting again.
                mConnectionResult = null;
                mPlusClient.connect();
            }
        }
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (mConnectionProgressDialog.isShowing()) {
        // The user clicked the sign-in button already. Start to resolve
        // connection errors. Wait until onConnected() to dismiss the
        // connection dialog.
        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(getActivity(),
                        REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                mPlusClient.connect();
            }
        }
    }

}

@Override
public void onConnected(Bundle connectionHint) {
    mConnectionProgressDialog.dismiss();
    Toast.makeText(getActivity().getApplicationContext(),
            "User is connected!", Toast.LENGTH_LONG).show();
    Intent intent = new Intent(getActivity().getApplicationContext(),
            LogoutActivity.class);
    startActivity(intent);

}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

}

我是与官方教程做的,然后再加工一点点地使用它的片段里面。

I was doing it with the official tutorial, and then reworking a little to use it inside fragment.

在主要活动,我有:

FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    googleSignUpFragment = new GoogleSignUpFragment();
    fragmentTransaction.add(R.id.detailFragment, googleSignUpFragment);
    fragmentTransaction.commit();

不过,每当我点击登录按钮,只有对话,说正在登录...,并没有什么事情发生。任何想法如何解决这个问题?它工作得很好内部正常的活动。

However whenever I click the log-in button, there is only dialog that says "Signing in..." and nothing else happens. Any ideas how to fix that problem? It was working just fine inside normal activity.

感谢您的帮助。

@Edit。 我已经想通了,该onConnectionFailed方法没有启动,所以没有onActivityResult看来,然而,当我把直接mPlusClient.connect()在我点击它的工作原理,但我觉得它不是一个正确的方式来做到这一点。也许问题是关于onConnectionFailed不beign推出

@Edit. I have figured out that the onConnectionFailed method is not launched, and so there is no onActivityResult it seems, however when I put directly mPlusClient.connect() inside my click it works, but I feel like its not a proper way to do that. Maybe the problem is about that onConnectionFailed not beign launched

@Edit 2。 现在,我已经想通了,这个问题似乎与dialog.isShowing()的,当我删除行,帐户选择是显示,起动之后,试图与布尔值,我点击后更改但是,这并不工作太......

@Edit 2. Now I have figured out that the problem seems to be with dialog.isShowing() as, when I remove the line, account chooser is showing, right after start, tried with bool value, that I change after click however this does not work too...

推荐答案

为了使用片段与官方教程,你必须知道onActivityResult方法的活动拥有该片段中被调用。

In order to use Fragments with the official tutorial, you have to know that the onActivityResult method will be called within the Activity owning the Fragment.

在片段然后实现公共连接()方法,你该活动的onActivityResult中调用调用,而不是 plus.connect(); 直接。这样turotial将使用碎片的工作,完全按照计划使用的活动时使用。如果你需要详细的例子,我可以添加编辑这个答案。

in the fragment then implement the public connect() method, which you call within the activity's onActivityResult instead of calling plus.connect(); directly. This way turotial will work with using fragments, exactly as intended to work when using Activity. If you need detailed example I can add the edit to this answer.

我会建议使用的活动,而不是由于code模块化,更容易实现和处理关键后退按钮以prevent进入应用程序,碎片等。

I would suggest using Activity and not Fragment due to code modularity and easier implementation and handling the on key back buttons to prevent going into the app, etc.

这篇关于在片段的Andr​​oid谷歌+登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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