【问题标题】:No getApiClient() in google play game services BaseGameUtilsgoogle play 游戏服务 BaseGameUtils 中没有 getApiClient()
【发布时间】:2023-04-07 03:24:02
【问题描述】:

我刚刚在我的游戏中添加了 google play services 排行榜,但是出现了一些问题。我无法开始排行榜活动。这是我的代码

startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), "leaderboard_id"), 5000);

问题是没有任何 getApiClient() 方法。有什么想法吗?

【问题讨论】:

    标签: java android google-play-services google-play-games


    【解决方案1】:

    您必须使用 GoogleApiClient 类型的对象。你的代码应该是这样的:

    public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener{
            GoogleApiClient mGoogleClient;
            ...
    
            @Override
            protected void onCreate(final Bundle savedInstanceState) {
                ...
                //Google Game 
                mGoogleClient = new GoogleApiClient.Builder(this, this, this)
               .addApi(Games.API)
               .addScope(Games.SCOPE_GAMES)
               .setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL)
               .build();
    
            }
    
            @Override
            public void onStart(){
                super.onStart();
                //Here is a good place to connect with Google Api
                mGoogleClient.connect();
            }
    
            @Override
            public void onConnected(Bundle connectionHint) {}
    
            @Override
            public void onConnectionSuspended(int cause) {}
    
            @Override
            public void onConnectionFailed(ConnectionResult connectionResult) {}
    
    }
    

    【讨论】:

      【解决方案2】:

      Games APIs doc指向Android Samples项目中BaseGameUtils库中的这个方法以及getApiClientBuilder()等类似方法:

      “如果您的游戏想要向 BaseGameActivity 管理的 GoogleApiClient 添加其他 Google Play 服务 API 或范围,它可以通过在 BaseGameUtils 中调用 GameHelper 对象上的 getApiClientBuilder() 来实现”。

      我还没有在BaseGameUtils on github 中看到这些方法,我猜它们会在某个时候更新。

      更新:看起来 BaseGameUtils 中的 GameHelper 更新是 on its way

      【讨论】:

        【解决方案3】:

        如果您的主要活动扩展了 BaseGameActivity 之类的 - 公共类 MainActivity 扩展 BaseGameActivity

        然后您可以在该活动中的任何位置使用 getApiClient()。

        但是,如果您想从另一个活动(或非活动类)访问 getApiClient(),请执行此操作-

        公共类 MainActivity 扩展 BaseGameActivity{

        static GoogleApiClient myclient;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            myclient=getApiClient();
        } 
        

        ...}

        那么你只需要调用“MainActivity.myclient” 而不是来自其他类的“getApiClient()”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-20
          • 2014-02-02
          • 2013-10-17
          • 2013-06-09
          • 1970-01-01
          相关资源
          最近更新 更多