如何使带有计数器的Java中的android应用每24小时自动将整数递增1?

我想使应用程序每24小时将整数增加1,所以我设置了sharedpreferences以便在应用程序关闭时将值保存在应用程序中。

我如何获得该应用程序以计算打开时经过的天数?

我还希望按钮重置计数器。

我已经尝试搜索google,但是找不到针对我的问题的解决方案。

public class Mainactivity extends AppCompatactivity {
    private int days;
    Button bt;
    TextView textView;
    private SharedPreferences counter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        counter = getapplicationContext().getSharedPreferences("shareware",Context.MODE_PRIVATE);
        days = counter.getInt("days",0);

        bt = (Button) findViewById(R.id.bt);
        textView = (TextView) findViewById(R.id.textView);
        textView.setText(Integer.toString(days));
        bt.setOnClicklistener(new View.OnClicklistener() {
            @Override
            public void onClick(View view) {
                days++;
                textView.setText(Integer.toString(days));
                counter.edit().putInt("days",days).apply();
                Log.i("Test: ",Integer.toString(counter.getInt("days",days)));
                Button bt = (Button)findViewById(R.id.bt);
                bt.setText("Reset");
            }
        });
    }
}
chyumkps198371 回答:如何使带有计数器的Java中的android应用每24小时自动将整数递增1?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3154268.html

大家都在问