1

I am working on app which is having Google+ integration in android app, I have tried to login with Google+ account, now PlusClient can not connect with account, what I did so far.

PlusClient mPlusClient;
mPlusClient.connect();

When I have checked that mPlusClient is connected or not, I got below resilt.

Log.i("PlusClient", ""+mPlusClient.isConnected());

Output is **False**.

Any help would be appreciated.

4

2 回答 2

1

希望它对您有所帮助..这个博客很好地描述了 Google+ 集成

http://ankitthakkar90.blogspot.in/

于 2013-05-22T04:11:27.557 回答
0

在开始之前阅读此内容:https ://developers.google.com/+/mobile/android/getting-started 然后此内容:https ://developers.google.com/+/mobile/android/sign-in

你需要初始化:

  • 在 Activity.onCreate 处理程序中初始化 PlusClient 对象。

  • 在 Activity.onStart() 期间调用 PlusClient.connect。

  • 在 Activity.onStop() 期间调用 PlusClient.disconnect。

您的活动将通过实现 ConnectionCallbacks 和 OnConnectionFailedListener 接口来侦听连接建立或失败的时间。

当 PlusClient 对象无法建立连接时,您的实现有机会在 onConnectionFailed 的实现中恢复,您将获得一个可用于解决任何连接失败的连接状态。您应该将此连接状态保存在成员变量中,并在用户按下登录按钮或 +1 按钮时通过调用 ConnectionResult.startResolutionForResult 来调用它。

@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(this,REQUEST_CODE_RESOLVE_ERR);
                   } catch (SendIntentException e) {
                           mPlusClient.connect();
                   }
           }
            }

   // Save the intent so that we can start an activity when the user clicks
   // the sign-in button.
   mConnectionResult = result;
   } 

      @Override
      public void onConnected() {
           // We've resolved any connection errors.
           mConnectionProgressDialog.dismiss();
            }

因为连接失败的解析是通过 startActivityForResult 和代码 REQUEST_CODE_RESOLVE_ERR 开始的,所以我们可以在 Activity.onActivityResult 中捕获结果。

    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
    mConnectionResult = null;
    mPlusClient.connect();
    }
    }

例如:

    import com.google.android.gms.common.*;
      import com.google.android.gms.common.GooglePlayServicesClient.*;
        import com.google.android.gms.plus.PlusClient;

      public class ExampleActivity extends Activity implements View.OnClickListener,
    ConnectionCallbacks, OnConnectionFailedListener {
private static final String TAG = "ExampleActivity";
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

private ProgressDialog mConnectionProgressDialog;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     //create an plusclient object 
    mPlusClient = new PlusClient.Builder(this, this, this)
            .setVisibleActivities("http://schemas.google.com/AddActivity", " http://schemas.google.com/BuyActivity")
            .build();
    // Progress bar to be displayed if the connection failure is not resolved.
    mConnectionProgressDialog = new ProgressDialog(this);
    mConnectionProgressDialog.setMessage("Signing in...");
}

@Override
protected void onStart() {
    super.onStart();
      //connect
    mPlusClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
     //disconnect
    mPlusClient.disconnect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (result.hasResolution()) {
        try {
            //start Solution for connectivity problems
            result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
        } catch (SendIntentException e) {
            mPlusClient.connect();
        }
    }
    // Save the result and resolve the connection failure upon a user click.
    mConnectionResult = result;
}

@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
        mConnectionResult = null;
        //Try connect again 
       mPlusClient.connect();
    }
}

@Override
public void onConnected() {
    //Get account name
    String accountName = mPlusClient.getAccountName();
    Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
}

@Override
public void onDisconnected() {
    Log.d(TAG, "disconnected");
}
  }

您还可以在 xml 中添加一个按钮以登录并在类中设置一个侦听器findViewById(R.id.sign_in_button).setOnClickListener(this);

  <com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
于 2013-05-22T03:19:05.540 回答