11
    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.plus.GooglePlusUtil;
    import com.google.android.gms.plus.PlusClient;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.content.IntentSender.SendIntentException;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends Activity implements ConnectionCallbacks,
            OnConnectionFailedListener {

        private static final int REQUEST_CODE_RESOLVE_ERR = 7;
        private ProgressDialog mConnectionProgressDialog;
        private PlusClient mPlusClient;
        private ConnectionResult mConnectionResult;
        private String TAG = "GmailLogin";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            int errorCode = GooglePlusUtil.checkGooglePlusApp(this);
            if (errorCode != GooglePlusUtil.SUCCESS) {
                GooglePlusUtil.getErrorDialog(errorCode, this, 0).show();
            } else {

                 mPlusClient = new PlusClient.Builder(this, this, this)
                 .setVisibleActivities( "http://schemas.google.com/AddActivity",
                 "http://schemas.google.com/BuyActivity").build();


                mConnectionProgressDialog = new ProgressDialog(this);
                mConnectionProgressDialog.setMessage("Signing in...");

                Button signInButton = (Button) findViewById(R.id.sign_in_button);
                signInButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        if (mConnectionResult == null) {
                            mConnectionProgressDialog.show();
                        } else {
                            try {
                                mConnectionResult
                                        .startResolutionForResult(
                                                MainActivity.this,
                                                REQUEST_CODE_RESOLVE_ERR);
                            } catch (SendIntentException e) {
                                // Try connecting again.
                                mConnectionResult = null;
                                mPlusClient.connect();
                            }
                        }
                    }
                });
            }

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @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() {
            String accountName = mPlusClient.getAccountName();
            Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG)
                    .show();
        }

        @Override
        public void onDisconnected() {
            Log.d(TAG, "disconnected");
        }

        @Override
        protected void onStart() {
            super.onStart();
            mPlusClient.connect();
        }

        @Override
        protected void onStop() {
            super.onStop();
            mPlusClient.disconnect();
        }

        }

我无法编译这段代码,因为它说:

PlusClient.Builder 无法解析为类型

mPlusClient = new PlusClient.Builder(this, this, this)
        .setVisibleActivities( "http://schemas.google.com/AddActivity",
        "http://schemas.google.com/BuyActivity").build();

也在onConnected()

null得到accountName

String accountName = mPlusClient.getAccountName();
4

4 回答 4

10

如果有人仍然有这个问题: PlayClient 现在已弃用。这里描述了“新”的方式:http ://android-developers.blogspot.com/2014/02/new-client-api-model-in-google-play.html

mClient = new GoogleApiClient.Builder(this)
        .addApi(Drive.API)
        .addScope(Drive.SCOPE_FILE)
        .addApi(Plus.API, plusOptions)
        .addScope(Plus.SCOPE_PLUS_LOGIN)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
于 2014-12-14T14:06:42.787 回答
7

正如 Luigi04 所述,该错误是因为 PlayClient 现在已被弃用。

作为一种解决方法,我通过将 gradle 依赖项中的播放服务版本降级为 来完成这项工作'com.google.android.gms:play-services:6.1.71',如此处所述: https ://stackoverflow.com/a/27637059/2161698

于 2015-01-02T17:41:48.070 回答
1

我遇到了同样的问题,问题是我使用的是旧版本的 Google Play 服务。所以请更新到最新版本。请记住,新版本已下载到 sdk 文件夹中。如果您之前已将 google-play-services 导入到您的工作区,则必须将其删除并添加新版本

于 2013-03-11T12:29:48.763 回答
0

The jar by itself cannot be use. The entire google play services lib project needs to be imported ie copied from SDK.

See step 5 of Google+ Quickstart:

5. Import the Google Play Services library project.

Select File > Import > Android > Existing Android Code Into Workspace and click Next.
Select Browse.... Enter <android-sdk-folder>/extras/google/google_play_services/.
Select google-play-services_lib. Click Finish to import.
于 2014-04-10T08:01:45.943 回答