在我的Android应用中集成Google plus

时间:2013-06-14 09:58:47

标签: android google-plus

我需要在我的应用程序中集成G​​oogle plus。我提到了Google开发者网站https://developers.google.com/+/mobile/android/sign-in。我在Google控制台中创建了一个产品并获得了客户端ID。我使用了该站点中给出的PlusClient代码。我没有在我的设备中运行。在该代码中,他们也没有使用客户端ID。我想要做的就是登录Google plus,就像我们以前在Twitter上登录一样,使用webwiew登录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());
                }
            }
    }
}

1 个答案:

答案 0 :(得分:0)

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

  1. 存储用户帐户信息,因此您无需在每次需要时与Google联系。
  2. 设置用户通过身份验证的sharedPerference值
  3. 为用户提供注销(更新sharedPerference)和断开连接的方法(删除用户断开连接时存储的信息)
  4. 在onConnected中,删除用于执行帖子的代码
  5. 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);

    希望这会有所帮助。