【问题标题】:How to reset counter?如何重置计数器?
【发布时间】:2016-06-04 13:31:04
【问题描述】:

我想在应用启动次数达到 10 次时展示插页式广告。所以我用

来计算它们
OnCreate{
 prefs = getPreferences(Context.MODE_PRIVATE);
    editor = prefs.edit();
    totalCount = prefs.getInt("counter", 0);
    totalCount++;
    editor.putInt("counter", totalCount);
    editor.commit(); }


每当 totalCount = 10 我运行添加:

       if(totalCount==10){

                fullScreenAd2.show();

            }


现在我想重置 totalCount,我该怎么做? 我知道调用 totalCount++ 会加 1 分,而 totalCount-- 会减 1 分。但是如何将其重置为 0?

【问题讨论】:

  • totalCount = 0 怎么样?
  • 不,这不起作用。
  • 不确定您的确切要求,但您使用 totalCount=0 重置它..
  • 不,这不会重置它。 :(
  • 当然可以!

标签: android count launch


【解决方案1】:

我认为你可以在它变成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
        }`

【讨论】:

    【解决方案2】:

    以下应该有效:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多