简体   繁体   中英

Android Studio - Need help looping code every second

I need a system that will execute this code every second. I've been looking for a while and still can't find any ways that work. I'm quite new to Java programming and programming for android so any help will be greatly appreciated.

Values n = new Values();

double num = n.getNum();
double adder = n.getAdder();
if(adder>=1) {
  num += (adder * 0.1);
}
TextView t = (TextView) findViewById(R.id.textView1);
t.setText(num + "");
n.setNum(num);

Also, I'm importing values from a Values class that looks like this,

package com.example.durtle02.durtle02;

public class Values {
  double num;
  double cost = 30;
  double adder;

  public double getNum() {
    return num;
  }

  public void setNum(double num) {
    this.num = num;
  }

  public double getCost() {
    return cost;
  }

  public void setCost(double cost) {
    this.cost = cost;
  }

  public double getAdder() {
    return adder;
  }

  public void setAdder(double adder) {
    this.adder = adder;
  }
}

EDIT

MainActivity.java

    package com.example.durtle02.durtle02;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import java.sql.Time;
    import java.util.*;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;


    public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //making game values accessible
    final Values n = new Values();
    //Loading The button for adding
    final Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double num = n.getNum();
            num++;
            TextView tt = (TextView) findViewById(R.id.textView1);
            n.setNum(num);
            tt.setText(n.getNum() + "");
        }
    });
    final Button button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double num = n.getNum();
            double cost = n.getCost();
            double adder = n.getAdder();
            if (num >= cost) {
                num -= cost;
                cost = cost * 1.35;
                adder++;
                cost = Math.round(cost);
                n.setNum(num);
                n.setCost(cost);
                n.setAdder(adder);
                TextView tl = (TextView) findViewById(R.id.textView3);
                tl.setText(n.getCost() + "");
                TextView tk = (TextView) findViewById(R.id.textView4);
                tk.setText(n.getAdder() + " Adders");
                TextView tt = (TextView) findViewById(R.id.textView1);
                tt.setText(n.getNum() + "");
                TextView tp = (TextView) findViewById(R.id.textView2);
                tp.setText((n.getAdder() * 0.1) + "/s");
            }
        }
    });
   countDownTimer.start();
}

 // Do something each second in a time frame of 60 seconds.
CountDownTimer countDownTimer = new CountDownTimer(60000, 100) {
    public void onTick(long millisUntilFinished) {
        Values n = new Values();

        double num = n.getNum();
        double adder = n.getAdder();
        if(adder>=1) {
            num += (adder * 0.1);
        }
        num++;
        n.setNum(num);
    }
    public void onFinish() {
        countDownTimer.start(); // restart again.
    }
};

        /*
        //---------------------------------------------------------------
        Values n = new Values();

        double num = n.getNum();
        double adder = n.getAdder();
        if(adder>=1) {
            num += (adder * 0.1);
        }
        TextView t = (TextView) findViewById(R.id.textView1);
        t.setText(num + "");

        n.setNum(num);
        //--------------------------------------------------------------
        */

    }

If your activity is in the foreground, the following code will do.

Handler mHandler= new Handler()
final Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // do your stuff here, called every second
        mHandler.postDelayed(this, 1000);
    }
};

// start it with:
mHandler.post(runnable);

I think you can use CountDownTimer , something like this:

// Do something each second in a time frame of 60 seconds.
CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
  public void onTick(long millisUntilFinished) {
    // For every second, do something.
    doSomething();
  }

  public void onFinish() {
    countDownTimer.start(); // restart again.
  }
}.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM