在所有活动中访问GoogleApiClient对象 [英] Accessing GoogleApiClient object in All Activities

查看:90
本文介绍了在所有活动中访问GoogleApiClient对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于大多数人来说,这似乎是一件简单的事情,如果他们想在其应用程序:s中使用Google Plus登录.

This seems like a simple thing that most people would need if they wanted to use Google Plus sign in with their application :s.

在活动1中:

我登录了用户.

登录后,我想使该用户对象可全局访问,因此我将其添加到Application对象:

After the sign in, I want to make that user object globally accessible, so I add it to the Application object:

public class GlobalUserAccess extends Application {

    private GoogleApiClient mGoogleApiClient;

    public GlobalUserAccess(){
        mGoogleApiClient = null;
    }

    public void setClient(GoogleApiClient client){
        mGoogleApiClient = client;
    }

    public GoogleApiClient getClient(){
        return mGoogleApiClient;
    }
}

通过这样绑定它:

GlobalUserAccess client = ((GlobalUserAccess) getApplicationContext());
client.setClient(mGoogleApiClient);

但是,当我尝试在活动2中访问它时:

However, when I try to access it in Activity 2:

GlobalUserAccess client = ((GlobalUserAccess) getApplicationContext());
String currentUser = Plus.AccountApi.getAccountName(client.getClient());

我得到了错误:

E/GMPM: getGoogleAppId failed with status: 10

有人可以请我填写正确的方法来完成此操作吗?我想让所有类都可以使用该用户对象,而我在此上花了太多时间:|.

Can someone please fill me in on the proper way to accomplish this? I'd like to have that user object available to all classes and I've spent way too much time on this :|.

我在某个地方搞砸了吗?啊...

Did I mess up somewhere? Ah...

来自活动1的客户端创建代码

client creation code from Activity 1

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(Plus.API)
        .addScope(new Scope(Scopes.PROFILE))
        .addScope(new Scope(Scopes.EMAIL))
        .build();

我直接从其Git存储库中使用Google的代码.它成功登录并在活动1中获取帐户信息.

I'm using Googles code directly from their Git repository. It successfully signs in and gets the account info in Activity 1.

推荐答案

在清单文件中添加google play位置服务依赖性和位置权限

Add google play location services dependency and location permission in manifest file

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

app/build.gradle

compile 'com.google.android.gms:play-services-location:11.0.0'

GoogleApiHelper.java

public class GoogleApiHelper implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
        private static final String TAG = GoogleApiHelper.class.getSimpleName();
        private Context context;
        private GoogleApiClient mGoogleApiClient;
        private ConnectionListener connectionListener;
        private Bundle connectionBundle;

        public GoogleApiHelper(Context context) {
            this.context = context;
            buildGoogleApiClient();
            connect();
        }

        public GoogleApiClient getGoogleApiClient() {
            return this.mGoogleApiClient;
        }

        public void setConnectionListener(ConnectionListener connectionListener) {
            this.connectionListener = connectionListener;
            if (this.connectionListener != null && isConnected()) {
                connectionListener.onConnected(connectionBundle);
            }
        }

        public void connect() {
            if (mGoogleApiClient != null) {
                mGoogleApiClient.connect();
            }
        }

        public void disconnect() {
            if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
                mGoogleApiClient.disconnect();
            }
        }

        public boolean isConnected() {
            return mGoogleApiClient != null && mGoogleApiClient.isConnected();
        }

        private void buildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(context)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API).build();

        }

        @Override
        public void onConnected(Bundle bundle) {
            connectionBundle = bundle;
            if (connectionListener != null) {
                connectionListener.onConnected(bundle);
            }
        }

        @Override
        public void onConnectionSuspended(int i) {
            Log.d(TAG, "onConnectionSuspended: googleApiClient.connect()");
            mGoogleApiClient.connect();
            if (connectionListener != null) {
                connectionListener.onConnectionSuspended(i);
            }
        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Log.d(TAG, "onConnectionFailed: connectionResult = " + connectionResult);
            if (connectionListener != null) {
                connectionListener.onConnectionFailed(connectionResult);
            }
        }

        public interface ConnectionListener {
            void onConnectionFailed(@NonNull ConnectionResult connectionResult);

            void onConnectionSuspended(int i);

            void onConnected(Bundle bundle);
        }
    }

App.java

public class App extends Application {
    private GoogleApiHelper googleApiHelper;
    private static App mInstance;

    @Override
    public void onCreate() {
        super.onCreate();

        mInstance = this;
        googleApiHelper = new GoogleApiHelper(mInstance);
    }

    public static synchronized App getInstance() {
        return mInstance;
    }

    public GoogleApiHelper getGoogleApiHelperInstance() {
        return this.googleApiHelper;
    }
    public static GoogleApiHelper getGoogleApiHelper() {
        return getInstance().getGoogleApiHelperInstance();
    }
}

注意:不要忘记在您的AndroidManifest.xml标记中将此子类的标准名称指定为"android:name"属性.

Note: Don't forget to specifying the fully-qualified name of this subclass as the "android:name" attribute in your AndroidManifest.xml's tag.

您可以通过回调获取apiClient并获取连接的时间

You can get apiClient by callback and get when it will connect

App.getGoogleApiHelper().setConnectionListener(new GoogleApiHelper.ConnectionListener() {
            @Override
            public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

            }

            @Override
            public void onConnectionSuspended(int i) {

            }

            @Override
            public void onConnected(Bundle bundle, GoogleApiClient googleApiClient) {
                //this function will call whenever google api connected or already connected when setting listener
                //You are connected do what ever you want
                //Like i get last known location
                Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
            }
        });

或者您也可以像这样

if(App.getGoogleApiHelper().isConnected())
{
    //Get google api client from anywhere
    GoogleApiClient client = App.getGoogleApiHelper().getGoogleApiClient();
}

这篇关于在所有活动中访问GoogleApiClient对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆