我尝试使用许多文档和资源,但找不到将 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/