【问题标题】:How to pass Googleapiclient to another all activity for Google Play Games如何将 Googleapiclient 传递给 Google Play 游戏的另一个所有活动
【发布时间】:2018-01-19 22:57:12
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    如果您需要经常连接到 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;
        }
    }
    

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

    在应用程序类文件中创建对象。不要忘记在清单中添加应用程序。 然后在我的第二个活动中创建一个新的 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();
    

    在清单中更新

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

    【讨论】:

    • 我像第二个答案一样添加了 App.java,我将把这个方法 GoogleApiClient client=App.getInstance().getClient() 放在另一个活动中;但它显示红色下划线,无法解析或创建 Getter 或创建属性。我是否需要创建 Application.java 以及如何将 App.java 放入 AndroidManifestfile
    • 请回复 pannunga 先生
    • 请回复
    • @KrishnakumarR 检查我更新的答案,仍然有问题,然后添加一些代码以便我检查。如果答案有帮助,请接受并投票:)
    【解决方案2】:

    我通过在所有活动中提供登录代码解决了该错误。 这样我就解决了登录一项活动并提交分数,解决了另一项活动错误。 Google Play Games 登录是通过使用以下链接实现的。

    https://developers.google.com/games/services/training/signin

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多