繁体   English   中英

Android:Google+整合。 仍保持连接状态时调用connect(),缺少断开连接()

[英]Android: Google+ integration. Calling connect() while still connected, missing disconnect()

我正在开发在Android应用程序中集成了Google+的应用程序,我尝试使用Google+帐户登录 ,现在PlusClient无法连接到帐户 ,到目前为止我所做的。

PlusClient mPlusClient;
mPlusClient.connect();

当我检查mPlusClient是否已连接时,我无法进行重新调整。

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

Output is **False**.

任何帮助,将不胜感激。

希望对您有帮助。.此博客很好地描述了Google+集成

http://ankitthakkar90.blogspot.in/

开始之前阅读以下内容: 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);在类中设置侦听器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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM