Android currentTimeMillis

时间:2013-08-03 14:03:03

标签: java android system add

你好我有一个启动System.currentTimeMillis的应用程序,因为它知道你开始使用应用程序多久了,它运行良好

但我想要做的是将保存的millis添加到当前系统millis,因此在加载数据时它将从停止的位置继续。

long starttime = System.currentTimeMillis(), millis;
int seconds, minutes, hours;

final Handler h = new Handler(new Callback() 
{
  public boolean handleMessage(Message msg) 
  {

           millis = System.currentTimeMillis() - starttime;
           seconds = (int) (millis / 1000);
           minutes = seconds / 60;
           hours = minutes / 60;
           seconds = seconds % 60;
           minutes = minutes % 60;


       playtime.setText(String.format("%d:%02d:%02d", hours, minutes, seconds));
       return false;
    }
});
class firstTask extends TimerTask 
{
     @Override
     public void run() 
     {
         h.sendEmptyMessage(0);
     }
};
Timer timer = new Timer();

3 个答案:

答案 0 :(得分:1)

谢谢你激励我的家伙,我提出了一个解决方案

long starttime = 0, millis, time_saved = 0;
int seconds, minutes, hours, stime = 0;

final Handler h = new Handler(new Callback() 
{
   public boolean handleMessage(Message msg) 
    {
       if(stime == 0)
       {
           millis = System.currentTimeMillis() - starttime;
           seconds = (int) (millis / 1000);
           minutes = seconds / 60;
           hours = minutes / 60;
           seconds = seconds % 60;
           minutes = minutes % 60;
       }
       else
       {
           millis = (System.currentTimeMillis() - starttime) + time_saved;
           seconds = (int) (millis / 1000);
           minutes = seconds / 60;
           hours = minutes / 60;
           seconds = seconds % 60;
           minutes = minutes % 60;
       }


       playtime.setText(String.format("%d:%02d:%02d", hours, minutes, seconds));
       return false;
    }
});
class firstTask extends TimerTask 
{
     @Override
     public void run() 
     {
         h.sendEmptyMessage(0);
     }
};
Timer timer = new Timer();

在方法

private void ST()
{
    time_saved = millis;
}

当我想要恢复时,我称之为

private void resumetime()
{
    starttime = System.currentTimeMillis();
    stime = 1;
}

它运作良好

答案 1 :(得分:0)

我将解释一个使用SharedPreferences的示例程序。

创建一个Android项目。

现在,这是我的 MainActivity.java

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

    SharedPreferences pref;
    long uptime,starttime,endtime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // now, I'm getting app uptime in seconds.
         pref = PreferenceManager.getDefaultSharedPreferences(this);
        Toast.makeText(this, "Up time:"+pref.getString("time", "0"), Toast.LENGTH_SHORT).show();
         uptime=Long.parseLong(pref.getString("time", "0"));
         starttime = System.currentTimeMillis()/1000;

    }
    @Override
    protected void onDestroy() {
        endtime=System.currentTimeMillis()/1000;
        uptime=uptime+(endtime-starttime);
        final SharedPreferences.Editor editor = pref.edit();
        editor.putString("time", ""+uptime);
        editor.commit();
        super.onDestroy();
    }



}

答案 2 :(得分:0)

基本上,您想要的是节省在每个会话中花费的时间并将其添加到之前的测量中,依此类推。共享首选项是一种很好的方法。我的建议是创建一个类如下的类:

public class AppPreferences {
    private static final String APP_SHARED_PREFS = "com.your.app";

    private SharedPreferences appSharedPrefs;
    private Editor prefsEditor;


    /**
     * 
     * @param context
     */
    public AppPreferences(Context context) {
        this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS,
                Activity.MODE_PRIVATE);
        this.prefsEditor = appSharedPrefs.edit();

    }
}

然后,将类似的方法放在你的班级中:

public void saveTimeSpentOnSession(long time){

    long timeSpentSoFar = getTimeSpentSoFar()
    // or you can do
    // long timeSpentSoFar = appSharedPrefs.getLong("timeSpent",0);
    prefsEditor.putLong("timeSpent",time+timeSpentSoFar);
    prefsEditor.commit();

}

public long getTimeSpentSoFar(){
    return appSharedPrefs.getLong("timeSpent",0);
}

无论何时想要调用put / get方法,只需创建此类的实例,如:

AppPreferences prefs = new AppPreferences(getApplicationContext()); // in an activity

并致电

prefs.saveTimeSpentOnSession(yourTime);