设置Google Play服务时出错

时间:2014-04-29 04:48:20

标签: android google-play-services

我正在尝试使用他们的android-sample项目设置Google Play服务。 我已成功导入 BaseGameUtils 以及 google_play_services_lib 。之后我导入了示例项目收集所有星星(并将 BaseGameUtils 添加到其构建目标中)以进行测试。 但它在以下两个导入

上给我错误( - 无法解决)
import com.google.android.gms.appstate.AppStateClient;
import com.google.android.gms.appstate.OnStateLoadedListener;

我甚至尝试导入其他示例项目,但它们都显示相同的错误。 下面是错误的快照。 enter image description here

3 个答案:

答案 0 :(得分:0)

只需重新启动eclipse即可构建工作空间。这有助于解决这个问题。

答案 1 :(得分:0)

我认为你的问题是google Play服务库项目没有更新

在打开sdk管理器后,从项目中删除库并下载Google Play服务的所有更新

然后从以下路径导入google play服务库项目

..../sdk/extras/google/google_play_services/libproject/google-play-services_lib

到您的工作区并构建并将此项目作为库项目添加到您的应用程序项目

清理你的应用程序并构建并运行它应该工作.....

答案 2 :(得分:0)

就我而言,问题出在谷歌播放服务的版本中。我更新到版本play-services:4.3.23,其界面略有不同 - 例如,删除了OnStateLoadedListener。

我通过更新代码来解决问题,以符合4.3.23版本的播放服务。

Former Cloud Save tutorial (now deprecated)

Old Activity sample in BaseGameUtils (now deprecated in favour of Saved Games snapshot)

不要在代码中使用OnStateLoadedListener,而是在从云端更新数据时设置ResultCallback<AppStateManager.StateResult>

(代码样本取自上述网站)

ResultCallback<AppStateManager.StateResult> mResultCallback = new
        ResultCallback<AppStateManager.StateResult>() {
    @Override
    public void onResult(AppStateManager.StateResult result) {
        AppStateManager.StateConflictResult conflictResult = result.getConflictResult();
        AppStateManager.StateLoadedResult loadedResult = result.getLoadedResult();
        if (loadedResult != null) {
            processStateLoaded(loadedResult);
        } else if (conflictResult != null) {
            processStateConflict(conflictResult);
        }
    }
};

void loadFromCloud() {
    mLoadingDialog.show();
    AppStateManager.load(getApiClient(), OUR_STATE_KEY).setResultCallback(mResultCallback);
}

void saveToCloud() {
    AppStateManager.update(getApiClient(), OUR_STATE_KEY, mSaveGame.toBytes());
    // Note: this is a fire-and-forget call. It will NOT trigger a call to any callbacks!
}

private void processStateConflict(AppStateManager.StateConflictResult result) {
    // Need to resolve conflict between the two states.
    // We do that by taking the union of the two sets of cleared levels,
    // which means preserving the maximum star rating of each cleared
    // level:
    byte[] serverData = result.getServerData();
    byte[] localData = result.getLocalData();

    SaveGame localGame = new SaveGame(localData);
    SaveGame serverGame = new SaveGame(serverData);
    SaveGame resolvedGame = localGame.unionWith(serverGame);

    AppStateManager.resolve(getApiClient(), result.getStateKey(), result.getResolvedVersion(),
            resolvedGame.toBytes()).setResultCallback(mResultCallback);
}

private void processStateLoaded(AppStateManager.StateLoadedResult result) {
    mLoadingDialog.dismiss();
    switch (result.getStatus().getStatusCode()) {
    case AppStateClient.STATUS_OK:
        // Data was successfully loaded from the cloud: merge with local data.
        mSaveGame = mSaveGame.unionWith(new SaveGame(result.getLocalData()));
        mAlreadyLoadedState = true;
        hideAlertBar();
        break;
    case AppStateClient.STATUS_STATE_KEY_NOT_FOUND:
        // key not found means there is no saved data. To us, this is the same as
        // having empty data, so we treat this as a success.
        mAlreadyLoadedState = true;
        hideAlertBar();
        break;
    case AppStateClient.STATUS_NETWORK_ERROR_NO_DATA:
        // can't reach cloud, and we have no local state. Warn user that
        // they may not see their existing progress, but any new progress won't be lost.
        showAlertBar(R.string.no_data_warning);
        break;
    case AppStateClient.STATUS_NETWORK_ERROR_STALE_DATA:
        // can't reach cloud, but we have locally cached data.
        showAlertBar(R.string.stale_data_warning);
        break;
    case AppStateClient.STATUS_CLIENT_RECONNECT_REQUIRED:
        // need to reconnect AppStateClient
        reconnectClient();
        break;
    default:
        // error
        showAlertBar(R.string.load_error_warning);
        break;
    }

    updateUi();
}