我正在使用在Android应用中集成 Google + 的应用,我尝试登录并使用 Google + 帐户,现在 PlusClient无法与帐户连接,我到目前为止所做的。
PlusClient mPlusClient;
mPlusClient.connect();
当我检查 mPlusClient 是否已连接时,我收到了以下信息。
Log.i("PlusClient", ""+mPlusClient.isConnected());
Output is **False**.
任何帮助将不胜感激。
答案 0 :(得分:1)
希望它对您有所帮助。这个博客很好地描述了Google+整合
答案 1 :(得分: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" />