Android:比较时间戳并添加到计时器

时间:2018-09-14 13:28:16

标签: android timestamp chronometer

如果我单击按钮btnTourStart,则会将时间戳保存到.txt文件中。在这种情况下,1536930346现在我想用System.currentTimeMillies();减去此时间戳以得到此结果并将其添加到我的chronometer(某种时间跟踪)中,尽管每次我得到一个像580:50:00(hh:mm:ss)这样的疯狂结果

这是到目前为止我得到的:

-读取文件-

btnLOAD.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {

                File root5 = Environment.getExternalStorageDirectory();
                File file5 = new File(root5, "TourState.txt");
                final StringBuilder text = new StringBuilder();
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file5));
                    String line;
                    while ((line = br.readLine()) != null) {
                        text.append(line);
                        // text.append('\n');
                    }
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

串成长串

long l = Long.parseLong(String.valueOf(text));

-然后我尝试减去它并将时间添加到我的天文钟,这是行不通的-

    long prevEventTime = System.currentTimeMillis();
    long l = Long.parseLong(String.valueOf(text));

    long elapsedRealtimeOff = prevEventTime - l;

chronometer1.setBase(elapsedRealtimeOff);
    chronometer1.start();
    chronometer1 = (Chronometer) findViewById(R.id.chronometer1);
    chronometer1.setFormat("HH:MM:SS");

0 个答案:

没有答案