我需要在我的应用程序中集成 Google plus。我提到了谷歌开发者网站https://developers.google.com/+/mobile/android/sign-in。我在 Google 控制台中创建了一个产品并获得了客户端 ID。我使用了该站点中提供的 PlusClient 代码。我不在我的设备中运行。在该代码中,他们也没有使用客户端 ID。我想做的就是登录 Google plus,就像我们过去使用 webwiew 登录 Twitter、Tumblr 并访问用户详细信息一样。
我使用了以下代码:
package com.googleplus.test;
import java.util.Iterator;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.PlusClient.OnPeopleLoadedListener;
import com.google.android.gms.plus.PlusShare;
import com.google.android.gms.plus.model.people.Person;
import com.google.android.gms.plus.model.people.PersonBuffer;
public class GooglePlusTest extends Activity implements android.view.View.OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener, OnPeopleLoadedListener {
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);
mPlusClient = new PlusClient.Builder(this, this, this).setScopes(Scopes.PLUS_LOGIN, Scopes.PLUS_PROFILE)
.setVisibleActivities("http://schemas.google.com/AddActivity").build();
// Progress bar to be displayed if the connection failure is not
// resolved.
mConnectionProgressDialog = new ProgressDialog(this);
mConnectionProgressDialog.setMessage("Signing in...");
// Button shareButton = (Button) findViewById(R.id.share_button);
//shareButton.setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
mPlusClient.connect();
}
@Override
protected void onStop() {
super.onStop();
mPlusClient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (result.hasResolution()) {
try {
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;
mPlusClient.connect();
}
}
@Override
public void onConnected(Bundle connectionHint) {
String accountName = mPlusClient.getAccountName();
// mPlusClient.loadPerson(this, "104242357859551899867");
//
// mPlusClient.loadPeople(GooglePlusTest.this, Person.Collection.VISIBLE);
Intent shareIntent = new PlusShare.Builder(this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
Toast.makeText(this, accountName + " is connected. try to load persons", Toast.LENGTH_LONG).show();
}
@Override
public void onDisconnected() {
Log.d(TAG, "disconnected");
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
if (mConnectionResult == null) {
mConnectionProgressDialog.show();
} else {
try {
mConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e) {
// Try connecting again.
mConnectionResult = null;
mPlusClient.connect();
}
}
}
else if (view.getId() == R.id.share_button ) {
Intent shareIntent = new PlusShare.Builder(GooglePlusTest.this)
.setType("text/plain")
.setText("Welcome to the Google+ platform.")
.setContentUrl(Uri.parse("https://developers.google.com/+/"))
.getIntent();
startActivityForResult(shareIntent, 0);
}
}
public void onPeopleLoaded(ConnectionResult status, PersonBuffer personBuffer, String nextPageToken) {
Log.i("", "persons loaded result = " + status.toString() + ", personsCount = " + personBuffer.getCount()
+ ", token = " + nextPageToken);
if (status.isSuccess()) {
Iterator<Person> itP = personBuffer.iterator();
while (itP.hasNext()) {
Person person = itP.next();
Log.i("", person.getNickname());
}
}
}
}