0

你好,我有一个应用程序,它会启动一个应用程序,以System.currentTimeMillis了解您自启动以来使用该应用程序多长时间,它运行良好

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

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

3 回答 3

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;
}

它工作得很好

于 2013-08-04T13:13:21.457 回答
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);
于 2013-08-03T20:27:19.100 回答
0

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

创建一个安卓项目。

现在,这是我的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();
    }



}
于 2013-08-03T15:38:17.147 回答