3

我需要在我的应用程序中集成 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());
                }
            }
    }
}
4

1 回答 1

0

从您可以发帖的事实来看,听起来您可以登录。如果您只想登录并执行发布,您可以:

  1. 存储用户帐户信息,这样您就无需在每次需要时都联系 Google。
  2. 设置用户通过身份验证的 sharedPerference 值
  3. 为用户提供注销(更新 sharedPerference)和断开连接(如果用户断开连接,删除您存储的信息)的方法
  4. 在onConnected中,去掉用来执行post的代码

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);

并在用户登录后调用您希望将用户带到的活动

Intent intent = new Intent(getApplicationContext(),YourActivity.class); startActivity(intent);

希望这可以帮助。

于 2013-08-04T11:49:12.497 回答