android - 如何将 Googleapiclient 传递给 Google Play 游戏的另一个所有 Activity

标签 android google-play-games google-api-client leaderboard

我尝试使用许多文档和资源,但找不到将 Googleapiclient 传递给另一个 Activity 的正确方法。 对我来说,Google play games sign in successful 是一项 Activity 。我想在另一项 Activity 中提交分数。 请给出如何完成我的项目的想法。

最佳答案

如果您需要经常连接到 GoogleApiClient,那么我可能会创建一个单例类来处理 GoogleApiClient 调用。

/**
 *  Class to maintain singlton object of google api
 */
public class GoogleApiClient_Singleton {
    private static final String TAG = "GoogleApiClient";
    private static GoogleApiClient_Singleton instance = null;

    private static GoogleApiClient mGoogleApiClient = null;

    protected GoogleApiClient_Singleton() {

    }

    public static GoogleApiClient_Singleton getInstance(GoogleApiClient aGoogleApiClient) {

        if(instance == null) {
            instance = new GoogleApiClient_Singleton();

            if (mGoogleApiClient == null)
                mGoogleApiClient = aGoogleApiClient;
        }

        return instance;
    }

    public GoogleApiClient get_GoogleApiClient(){
        return mGoogleApiClient;
    }
}

或者为了从任何地方访问对象,

在应用程序类文件中创建对象。不要忘记在 list 中添加应用程序。 然后在我的第二个 Activity 中创建一个新的 GoogleApiClient 实例,然后像这样获取 api 客户端

public class App extends Application {
    private static 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;
    }
}

// In second activity
GoogleApiClient client = App.getInstance().getClient();

更新 list

<application
    android:name=".App"
    ... other tags
    />

关于android - 如何将 Googleapiclient 传递给 Google Play 游戏的另一个所有 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45634946/

相关文章:

android - 使用按钮在运行时调整 ImageView 的大小 - Android

ios - GTLRDrive_File 对象缺少 modifiedTime 和 createdTime 属性

gradle - Google API客户端链接了错误版本的jackson-core

android - 尝试将 Google Play 游戏集成到我的应用程序时出现 API 异常

android - 在玩家未登录时处理 Google Play 游戏成就

Android Wear 访问 Google Fit API

android - Gradle Clean Build 和连接AndroidTest

java - 我在 SQLITE 中执行选择时出错

android - 自动点击提交按钮以及如何通过提交按钮的时间限制?

android - Unity - Google Play 游戏服务实时多人游戏关闭 WaitingRoomUI() 方法?