【问题标题】:Making app to count time since first time used让应用程序计算自第一次使用以来的时间
【发布时间】:2020-02-14 20:36:02
【问题描述】:

如何让应用程序从第一次打开开始计算时间?我不知道要使用哪个类。是否有可能用秒表或类似的东西来实现这一点?有人可以分享一些代码吗?我不知道在互联网上搜索什么只是对我想要构建什么有一个想法。或者只是告诉我一个想法,我可以实现这一目标以及如何实现这一目标?

 statisticLayout.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          if (plusiliminus==0){
              Toast.makeText(getActivity(),R.string.toaststatistic,Toast.LENGTH_SHORT).show();
          } else {
              Fragment fragmentstatistic=new Statistic();
              FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
              transaction.replace(R.id.frameLayout,fragmentstatistic).commit();
          }
      }
  });

【问题讨论】:

  • 您想跟踪应用程序每次启动的时间,还是仅记录第一次安装和打开的时间?
  • 我需要在第一次使用应用程序时获取时间,然后在按钮上重置,然后在 textview 中显示,但即​​使应用程序关闭,它也必须运行和计数
  • 您是否搜索过秒表或经过时间等字词?我想你想要 Duration 类。您还可以搜索如何将Duration 格式化为小时、分钟和秒。

标签: java android time elapsedtime


【解决方案1】:

据我所知,您在首次使用时需要持久存储。我认为SharedPreferences 是在您的情况下存储它的好方法。

在您的启动器活动的onCreate 函数中,您可能会获得System.currentTimeInMillis() 并将其存储在SharedPreferences as stated here 中。

那么当你需要重新设置时间时,你只需要清除SharedPreferences中的值,然后重新设置一个新的值即可。

我希望这能让您对自己的实现有所了解。

【讨论】:

    【解决方案2】:

    你基本上必须做两件事。

    获取启动应用程序的当前时间,或使用类似的方式按下按钮

    Date currentTime = Calendar.getInstance().getTime();
    

    这是一个好的开始,你有时间,然后你必须以你想向用户显示的格式解析这个时间,然后你管理一个停止和重置按钮,例如在这个 tutorial 中显示的p>

    因此,您构建了一个 TextView 并实时显示秒数,并以您需要的方式进行解析

    import android.os.Handler;  
    import android.os.SystemClock;  
    import android.support.v7.app.AppCompatActivity;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.widget.Button;  
    import android.widget.TextView;  
    
    public class MainActivity extends AppCompatActivity {  
    
        TextView timer ;  
        Button start, pause, reset;  
        long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L ;  
        Handler handler;  
        int Seconds, Minutes, MilliSeconds ;  
    
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
    
            timer = (TextView)findViewById(R.id.tvTimer);  
            start = (Button)findViewById(R.id.btStart);  
            pause = (Button)findViewById(R.id.btPause);  
            reset = (Button)findViewById(R.id.btReset);  
    
            handler = new Handler() ;  
    
            start.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View view) {  
    
                    StartTime = SystemClock.uptimeMillis();  
                    handler.postDelayed(runnable, 0);  
    
                    reset.setEnabled(false);  
    
                }  
            });  
    
            pause.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View view) {  
    
                    TimeBuff += MillisecondTime;  
    
                    handler.removeCallbacks(runnable);  
    
                    reset.setEnabled(true);  
    
                }  
            });  
    
            reset.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View view) {  
    
                    MillisecondTime = 0L ;  
                    StartTime = 0L ;  
                    TimeBuff = 0L ;  
                    UpdateTime = 0L ;  
                    Seconds = 0 ;  
                    Minutes = 0 ;  
                    MilliSeconds = 0 ;  
    
                    timer.setText("00:00:00");  
    
                }  
            });  
    
        }  
    
        public Runnable runnable = new Runnable() {  
    
            public void run() {  
    
                MillisecondTime = SystemClock.uptimeMillis() - StartTime;  
    
                UpdateTime = TimeBuff + MillisecondTime;  
    
                Seconds = (int) (UpdateTime / 1000);  
    
                Minutes = Seconds / 60;  
    
                Seconds = Seconds % 60;  
    
                MilliSeconds = (int) (UpdateTime % 1000);  
    
                timer.setText("" + Minutes + ":"  
                        + String.format("%02d", Seconds) + ":"  
                        + String.format("%03d", MilliSeconds));  
    
                handler.postDelayed(this, 0);  
            }  
    
        };  
    
    }  
    
    

    activity_main.xml的观点

     <?xml version="1.0" encoding="utf-8"?>  
        <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
            xmlns:app="http://schemas.android.com/apk/res-auto"  
            xmlns:tools="http://schemas.android.com/tools"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent"  
            android:background="@color/colorPrimary"  
            tools:context="in.amitsin6h.stopwatch.MainActivity">  
    
    
    
                <RelativeLayout  
                    android:layout_width="fill_parent"  
                    android:layout_height="fill_parent"  
                    android:layout_marginLeft="10dp"  
                    android:layout_marginRight="10dp"  
                    android:paddingBottom="90dp">  
    
    
                   <TextView  
                       android:text="00:00:00"  
                       android:layout_width="wrap_content"  
                       android:layout_height="wrap_content"  
                       android:id="@+id/tvTimer"  
                       android:textSize="50dp"  
                       android:textStyle="bold"  
                       android:textColor="#ffffff"  
                       android:layout_marginTop="120dp"  
                       android:paddingBottom="50dp"  
                       android:layout_alignParentTop="true"  
                       android:layout_centerHorizontal="true" />  
    
                   <Button  
                       android:text="Start"  
                       android:background="#ffffff"  
                       android:layout_width="wrap_content"  
                       android:layout_height="wrap_content"  
                       android:layout_below="@+id/tvTimer"  
                       android:layout_alignParentLeft="true"  
                       android:layout_alignParentStart="true"  
                       android:layout_marginTop="41dp"  
                       android:id="@+id/btStart" />  
    
                   <Button  
                       android:text="Pause"  
                       android:background="#ffffff"  
                       android:layout_width="wrap_content"  
                       android:layout_height="wrap_content"  
                       android:id="@+id/btPause"  
                       android:layout_alignBaseline="@+id/btStart"  
                       android:layout_alignBottom="@+id/btStart"  
                       android:layout_centerHorizontal="true" />  
    
                   <Button  
                       android:text="Reset"  
                       android:background="#ffffff"  
                       android:layout_width="wrap_content"  
                       android:layout_height="wrap_content"  
                       android:layout_alignTop="@+id/btPause"  
                       android:layout_alignParentRight="true"  
                       android:layout_alignParentEnd="true"  
                       android:id="@+id/btReset" />  
    
                </RelativeLayout>  
    
    
        </android.support.constraint.ConstraintLayout>  
    

    【讨论】:

    • 但我需要在 textview 小时、分钟、秒中显示这个时间
    • 是的,这在我给你的教程中显示,你创建一个 TextView 然后你将它绑定到视图,决定应该如何显示,你明白你要求一个教程,而不是写一个应用程序:) 堆栈溢出是针对一个问题,如果你得到一个块,而不是从头开始编写一个应用程序
    猜你喜欢
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2017-03-19
    • 2018-07-29
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多