Reset achievements/leaderboard from my Android application

僤鯓⒐⒋嵵緔 提交于 2019-12-20 20:38:13

问题


Can I reset my achievements/leaderboard result from my Android application that is used with Google Play Game Services?


回答1:


Yes, this is possible from test accounts. It’s a bit tricky so here some example code.

Add this in your manifest:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />

And here is the actual code to be executed in the application. Added in the activity that is based on 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);
    }
}



回答2:


There's another way you can achieve this. Go to your leaderboard(s) in the game services area of the developer site. Then choose one of your leaderboards. Change the Ordering option 'Larger is better' to 'Smaller is better' or vice versa. Save then go change it back.

This will wipe out any values you currently have for that leaderboard, and of course is only doable while testing.



来源:https://stackoverflow.com/questions/17658732/reset-achievements-leaderboard-from-my-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!