我能否通过使用与Google Play游戏服务配合的Android应用程序重置我的成就/排行榜结果?
是的,这可以通过测试账户实现。这有点棘手,因此以下是一些示例代码。
将以下内容添加到您的清单中:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
这里是应用程序中要执行的实际代码。添加在基于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);
}
}
你可以通过另一种方式实现这个目标。进入开发者网站的游戏服务区域,访问排行榜,选择其中一个排行榜,然后更改排序选项“较大为更好”或“较小为更好”。保存更改后再改回原来的选项。
这将清除该排行榜当前的任何值,当然只能在测试期间进行。