如何重置计数器?
How to reset counter?
我想在应用达到 10 次启动时展示插页式广告。所以我用
来计算它们
OnCreate{
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
totalCount = prefs.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.commit(); }
并且每当 totalCount = 10 i 运行 添加:
if(totalCount==10){
fullScreenAd2.show();
}
现在我想重置 totalCount,我该怎么做?
我知道调用 totalCount++ 会增加 1 分,而 totalCount-- 会减少 1 分。但是我怎样才能将它重置为 0?
我觉得你可以在变成10后重新设置为0,例如:
if(totalCount==10){
fullScreenAd2.show();
totalCount=0; // and i think that, this should do it...
editor.putInt("counter", totalCount); // call editor to save
editor.commit(); //totalCount's value
}`
以下应该有效:
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
totalCount = prefs.getInt("counter", 0);
if(totalCount == 10){
fullScreenAd2.show();
totalCount = 0;
} else {
totalCount++;
}
editor.putInt("counter", totalCount);
editor.commit();
我想在应用达到 10 次启动时展示插页式广告。所以我用
OnCreate{
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
totalCount = prefs.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.commit(); }
并且每当 totalCount = 10 i 运行 添加:
if(totalCount==10){
fullScreenAd2.show();
}
现在我想重置 totalCount,我该怎么做?
我知道调用 totalCount++ 会增加 1 分,而 totalCount-- 会减少 1 分。但是我怎样才能将它重置为 0?
我觉得你可以在变成10后重新设置为0,例如:
if(totalCount==10){
fullScreenAd2.show();
totalCount=0; // and i think that, this should do it...
editor.putInt("counter", totalCount); // call editor to save
editor.commit(); //totalCount's value
}`
以下应该有效:
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
totalCount = prefs.getInt("counter", 0);
if(totalCount == 10){
fullScreenAd2.show();
totalCount = 0;
} else {
totalCount++;
}
editor.putInt("counter", totalCount);
editor.commit();