从我的Android应用程序重置成就/排行榜

PEK*_*PEK 8 android google-play-games

我可以从与Google Play游戏服务一起使用的Android应用程序重置我的成就/排行榜结果吗?

PEK*_*PEK 15

是的,这可以从测试帐户中获得.这有点棘手,所以这里有一些示例代码.

在清单中添加:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)

这是在应用程序中执行的实际代码.在基于BaseGameActivity的活动中添加.

public void resetAchievements()
{
    if( isSignedIn() )
    {
        String accountName = getGamesClient().getCurrentAccountName();      
        String scopes = getScopes();

        new ResetterTask(this, accountName, scopes).execute((Void) null);
    }
}

private class ResetterTask extends AsyncTask<Void, Void, Void>
{
    public String mAccountName;
    public String mScope;
    public Context mContext;

    public ResetterTask(Context con, String name, String sc)
    {
        mContext = con;
        mAccountName = name;
        mScope = sc;
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        try
        {
            String accesstoken = GoogleAuthUtil.getToken(mContext, mAccountName, mScope);

            HttpClient client = new DefaultHttpClient();                
            //Reset leader board:
            /*String leaderboardid = "theleaderboardid";
            HttpPost post = new HttpPost
                    (
                        "https://www.googleapis.com"+
                        "/games/v1management"+
                        "/leaderboards/"+
                        leaderboardid+
                        "/scores/reset?access_token="+accesstoken
                    );*/

            //Reset a single achievement like this:
            /*
            String acheivementid = "acheivementid";
            HttpPost post = new HttpPost
                    (
                        "https://www.googleapis.com"+
                        "/games/v1management"+
                        "/achievements/"+
                        acheivementid+
                        "/reset?access_token="+accesstoken
                    );*/

            //This resets all achievements:
            HttpPost post = new HttpPost
                    (
                        "https://www.googleapis.com"+
                        "/games/v1management"+
                        "/achievements"+
                        "/reset?access_token="+accesstoken
                    );


            client.execute(post);
            Log.w(LogTag, "Reset achievements done.");
        }
        catch(Exception e)
        {
            Log.e(LogTag, "Failed to reset: " + e.getMessage(), e);
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        //Launch activity to refresh data on client.
        //NOTE: Incremental achievements will look like they are not reset.
        //However, next time you and some steps it will start from 0 and
        //gui will look ok.
        startActivityForResult(getGamesClient().getAchievementsIntent(), 0);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Will Calderwood这种方法仍然有效,但谷歌改变了他们的GamesClient api.您可以使用"yourtesteremail@gmail.com"获取电子邮件,使用"oauth2:h ttps://www.googleapis.com/auth/games"获取范围(nospace) (2认同)