在所有活动中访问GoogleApiClient对象

时间:2015-10-28 18:07:42

标签: android singleton google-plus

如果他们想要使用Google Plus登录他们的应用程序,这似乎是大多数人需要的简单事情:s。

在活动1中:

我在用户签名。

登录后,我希望全局访问该用户对象,因此我将其添加到Application对象中:

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中访问它时:

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

我收到错误:

E/GMPM: getGoogleAppId failed with status: 10

有人可以用正确的方式填写我吗?我希望所有课程都能使用该用户对象,而且我花了太多时间在这上面:|。

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

编辑:活动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存储库使用Googles代码。它成功登录并获取活动1中的帐户信息。

3 个答案:

答案 0 :(得分:25)

在清单文件中添加Google Play位置服务依赖关系和位置权限

<强>的AndroidManifest.xml

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

应用/的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();
    }
}

注意:不要忘记将此子类的完全限定名称指定为&#34; android:name&#34; AndroidManifest.xml标记中的属性。

您可以通过回调获取apiClient,并在连接时获取

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

答案 1 :(得分:4)

为了从每个地方访问对象,你可以像我在这里做的那样 App.java。不要忘记在清单中添加应用程序

<application
        android:name=".App"

现在访问对象只需从任何地方

进行访问
App.getGoogleApiHelper();

另请在此处查看我的GoogleApiHelper课程GoogleApiHelper.java

对于GooglePlus Api,请查看此GooglePlusAPI。请按照以下步骤操作。

根据您的代码的应用代码

public class App extends Application {
    private GoogleApiClient mGoogleApiClient;

    private static App mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

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

    public GoogleApiClient getClient(){
       return mGoogleApiClient;
    }
}

现在初始化并访问

App.getInstance().setClient(client);
GoogleApiClient client = App.getInstance().getClient();

答案 2 :(得分:0)

在您的活动1中,当您尝试获取Application对象时,您应该执行以下操作:

GlobalUserAccess client = ((GlobalUserAccess) getApplication());

然后使用GoogleApiClient方法设置setClient对象。在您的activity2中使用相同的方式来获取Application对象。