Android 共享引用和倒计时计时器

Android 共享引用和倒计时计时器,android,countdowntimer,Android,Countdowntimer,我想将倒计时Ttimer值保存到一个变量中,以便下次用户启动倒计时活动时,它可以在实际经过的时间重新加载。 这是我的代码,我不明白为什么它不起作用。(显示我的时间文本框为空) 如果我是对的,您正在更新共享首选项。这可能是个坏主意。最好将时间戳和计时器上的时间保存在桌面上,然后重新开始计算经过的时间并从中提取。我没有看到您的计时器变量在ontick()中使用,您使用了timer2变量。 在setText()行之前放置一个loggerLog.d(),以检查正在使用的变量的结果。。。否则,editTe

我想将倒计时Ttimer值保存到一个变量中,以便下次用户启动倒计时活动时,它可以在实际经过的时间重新加载。 这是我的代码,我不明白为什么它不起作用。(显示我的时间文本框为空)


如果我是对的,您正在更新共享首选项。这可能是个坏主意。最好将时间戳和计时器上的时间保存在桌面上,然后重新开始计算经过的时间并从中提取。

我没有看到您的
计时器
变量在ontick()中使用,您使用了
timer2
变量。 在setText()行之前放置一个logger
Log.d()
,以检查正在使用的变量的结果。。。否则,editText不能绝对为空

此外,在活动的onStop()中的共享首选项中节省时间。。不是在计时器的每个滴答声中

编辑: OP澄清要求后

您可能希望采用不同的方法。。 说什么时候开始倒计时。。使用System.currentTimeMillis()获取开始时间 并将其存储在SharedReferences/DB中

每当活动开始时获取开始时间frm首选项。。。启动计时器,初始时间为(System.currentTimeMillis-start\u time),然后单击()更新计时器(UI)。注意:现在不必将计时器值更新回SharedReferences:

  prefs = PreferenceManager.getDefaultSharedPreferences(this);

   xtime = System.currentTimeMillis()-prefs.getLong("TIME",System.currentTimeMillis()); 

   long timer=86400000-(xtime+prefs.getLong("TIME2",0)); 

   final SharedPreferences.Editor editor = prefs.edit();
   editor.putLong("TIME2",xtime+prefs.getLong("TIME2",0));  
   editor.commit(); 


   new CountDownTimer(timer, 1000) { 

         public void onTick(long elapsed) {

             Log.d(TAG, "TIMER" + System.currentTimeMillis());
             long timer2=elapsed;
             long hours = timer2 / hours_in_millies;
             timer2 %= hours_in_millies;
             long minutes = timer2 / minutes_in_millies;
             timer2 %= minutes_in_millies;
             long seconds = timer2 / seconds_in_millies;
             time.setText(hours + ":" + minutes + ":" + seconds);


         }

         public void onFinish() 
         { Intent intent = new Intent(Hug.this, Hug_Accepted.class);
        startActivity(intent);

         }

   }
   .start();    
   xtime=System.currentTimeMillis(); 
    SharedPreferences.Editor editor2 = prefs.edit();
     editor2.putLong("TIME",xtime);         
     editor2.commit(); 

}

ypu从你们的系统中得到了什么?SharedReferences正在工作?如果应用程序关闭,我该怎么办?如果应用程序关闭,应该调用onStop。此时您的pref将被保存。我应该在我的Back button pressed类中使用editor.commit()吗?还有,如果计时器用完了我该怎么办?我现在真的不知道如何使用你的idea Pyro,你能在上面写一个简单的代码吗?我已经看过日志,并将变量保存部分放在了我的OnBackPressed方法中。问题似乎出在longtimer=prefs.getLong(“TIME”,86400000),似乎它总是从86400000开始。你能显示你的更新代码吗。。。正确的方法是在onTick()中存储最新的min/hrs/。。。在本地(活动范围)变量中。。以及使用这些变量更新SharedReferences的代码……哦,现在我发现这不是我真正想要的。我只是想要一个倒计时,即使活动或应用程序关闭,它也会继续计时。有人能帮我吗?找到了一篇关于它的帖子,但它暗示了使用服务,我对此一无所知。是的,你可以使用服务。。我用另一种方法编辑了答案。。
  prefs = PreferenceManager.getDefaultSharedPreferences(this);

   xtime = System.currentTimeMillis()-prefs.getLong("TIME",System.currentTimeMillis()); 

   long timer=86400000-(xtime+prefs.getLong("TIME2",0)); 

   final SharedPreferences.Editor editor = prefs.edit();
   editor.putLong("TIME2",xtime+prefs.getLong("TIME2",0));  
   editor.commit(); 


   new CountDownTimer(timer, 1000) { 

         public void onTick(long elapsed) {

             Log.d(TAG, "TIMER" + System.currentTimeMillis());
             long timer2=elapsed;
             long hours = timer2 / hours_in_millies;
             timer2 %= hours_in_millies;
             long minutes = timer2 / minutes_in_millies;
             timer2 %= minutes_in_millies;
             long seconds = timer2 / seconds_in_millies;
             time.setText(hours + ":" + minutes + ":" + seconds);


         }

         public void onFinish() 
         { Intent intent = new Intent(Hug.this, Hug_Accepted.class);
        startActivity(intent);

         }

   }
   .start();    
   xtime=System.currentTimeMillis(); 
    SharedPreferences.Editor editor2 = prefs.edit();
     editor2.putLong("TIME",xtime);         
     editor2.commit(); 

}