Stucked on Loops, not sure what is the wrong - java

I'm a just a beginner to android & java. I'm trying to build an app that will tell you if you win or lose base on the Martingale on gambling.
My concept is, you can set your money, target and the minimum bet.
for example is if I set my current money is 1000, and my target is to get 1100, and the minimum bet is 100, the app will auto run the function for example 10 times and calculate the win rate.
now I'm stucked, the android studio shows no error, however, whenever i try to emulate the app, it show "unfortunately, app has stopped" everything seem just fine to me, i don't know how to find the error... please guide me. million thanks
package com.example.android.gambling;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EditText cMoney = (EditText) findViewById(R.id.money);
double currentMoney = Double.parseDouble(cMoney.getText().toString());
EditText target = (EditText) findViewById(R.id.target);
double theTarget = Double.parseDouble(target.getText().toString());
EditText bet = (EditText) findViewById(R.id.bet);
double minBet = Double.parseDouble(bet.getText().toString());
boolean findRate = calRate(currentMoney, theTarget, minBet);
public void seeRate(View view) {
TextView textview = (TextView)findViewById(R.id.textView);
textview.setText("You " + winPercentage());
}
public boolean calRate(double currentMoney, double theTarget, double minBet) {
while (currentMoney>minBet){
boolean win = winRate();
if (win){
currentMoney += minBet;
minBet = minBet;
}
else {
currentMoney -= minBet;
minBet *= 2;
}
if (currentMoney>=theTarget){
return true;
}
}
return false;
}
private boolean winRate() {
double d = Math.random();
if (d < 0.5)
return true;
else
return false;
}
public int winPercentage (){
int numberWin = 0;
for (int i=0; i<=10; i++){
boolean win = calRate(currentMoney, theTarget, minBet);
if (win){
numberWin = numberWin + 1;
}
}
return numberWin/10*100;
}
}
Edit(1)
package com.example.android.gambling;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText cMoney = (EditText) findViewById(R.id.money);
double currentMoney = Double.parseDouble(cMoney.getText().toString());
EditText target = (EditText) findViewById(R.id.target);
double theTarget = Double.parseDouble(target.getText().toString());
EditText bet = (EditText) findViewById(R.id.bet);
double minBet = Double.parseDouble(bet.getText().toString());
boolean findRate = calRate(currentMoney, theTarget, minBet);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void seeRate(View view) {
TextView textview = (TextView)findViewById(R.id.textView);
textview.setText("You " + winPercentage());
}
public boolean calRate(double currentMoney, double theTarget, double minBet) {
while (currentMoney>minBet){
boolean win = winRate();
if (win){
currentMoney += minBet;
minBet = minBet;
}
else {
currentMoney -= minBet;
minBet *= 2;
}
if (currentMoney>=theTarget){
return true;
}
}
return false;
}
private boolean winRate() {
double d = Math.random();
if (d < 0.5)
return true;
else
return false;
}
public int winPercentage (){
int numberWin = 0;
for (int i=0; i<=10; i++){
boolean win = calRate(currentMoney, theTarget, minBet);
if (win){
numberWin = numberWin + 1;
}
}
return numberWin/10*100;
}
}

Do this:
private EditText cMoney;
private double currentMoney;
private EditText target;
private double theTarget;
private EditText bet;
private double minBet;
private boolean findRate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cMoney = (EditText) findViewById(R.id.money);
currentMoney = Double.parseDouble(cMoney.getText().toString());
target = (EditText) findViewById(R.id.target);
theTarget = Double.parseDouble(target.getText().toString());
bet = (EditText) findViewById(R.id.bet);
minBet = Double.parseDouble(bet.getText().toString());
findRate = calRate(currentMoney, theTarget, minBet);
}
You can't put the code like this outside of any method/constructor, unless it's static/initializer block and there's definitely no reason to use them here.
Also, I see that your parameter name in calRate is currentMoney same as your variable, try to avoid this, it may be confusing for someone else reading your code, for you and maybe even for compiler if you go too far.

Put this:
EditText cMoney = (EditText) findViewById(R.id.money);
double currentMoney = Double.parseDouble(cMoney.getText().toString());
EditText target = (EditText) findViewById(R.id.target);
double theTarget = Double.parseDouble(target.getText().toString());
EditText bet = (EditText) findViewById(R.id.bet);
double minBet = Double.parseDouble(bet.getText().toString());
boolean findRate = calRate(currentMoney, theTarget, minBet);
Inside brackets of method onCreate() and then export variables theTarget, minBet, findRate and currentMoney into fields so that they can be visible in other parts of this class.

Related

How to use `onSaveInstanceState`?

What do I want to achieve?
I want to save values of x, y and score, so when the user rotates the screen then the values are retrieved and not regenerated.
What do I have?
Probably correct onSaveInstanceState code that isn't working probably because of some #Override coming into conflict with it
What do I need?
Some fix that will help me save values of variables
Code
MainActivity.java (without onSaveInstanceState)
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private EditText textBox;
private int z;
public int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
textBox = findViewById(R.id.editText);
textBox.requestFocus();
setActual();
}
private void setActual() {
Random random = new Random();
int x = random.nextInt(10) + 1; //you get a random number between 0 and 9 then add 1 to get 1 - 10, no need to use array..
int y = random.nextInt(10) + 1;
z = x * y;
String hintStr = x + " × " + y;
((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
}
public void onCheck(View view) {
int answer = Integer.parseInt(textBox.getText().toString());
String res = "Incorrect";
if (answer == z) {
res = "Correct";
score++;
Log.d("TAG", "Correct."); // right answer
textBox.setText("");
setActual(); // let's regenerate values
} else if (score == 0) {
Log.d("TAG", "Incorrect."); // wrong answer
textBox.setText("");
setActual();
((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
} else {
Log.d("TAG", "Incorrect."); // wrong answer
textBox.setText("");
score--;
setActual();
}
((TextView) findViewById(R.id.scoreText)).setText("Score:" + ' ' + score);
((TextView) findViewById(R.id.result_text_view)).setText(res);
}
}
MainActivity.java (with onSaveInstanceState)
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private EditText textBox;
private int z;
public int score = 0;
private int x;
private int y;
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("x", x);
savedInstanceState.putInt("y", y);
savedInstanceState.putInt("score", score);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = findViewById(R.id.editText);
textBox.requestFocus();
setActual();
if (savedInstanceState != null) {
x = savedInstanceState.getInt("x");
y = savedInstanceState.getInt("y");
score = savedInstanceState.getInt("score");
}
}
private void setActual() {
Random random = new Random();
x = random.nextInt(10) + 1; //you get a random number between 0 and 9 then add 1 to get 1 - 10, no need to use array..
y = random.nextInt(10) + 1;
z = x * y;
String hintStr = x + " × " + y;
((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
}
public void onCheck(View view) {
int answer = Integer.parseInt(textBox.getText().toString());
String res = "Incorrect";
if (answer == z) {
res = "Correct";
score++;
Log.d("TAG", "Correct."); // right answer
textBox.setText("");
setActual(); // let's regenerate values
} else if (score == 0) {
Log.d("TAG", "Incorrect."); // wrong answer
textBox.setText("");
setActual();
((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
} else {
Log.d("TAG", "Incorrect."); // wrong answer
textBox.setText("");
score--;
setActual();
}
((TextView) findViewById(R.id.scoreText)).setText("Score:" + ' ' + score);
((TextView) findViewById(R.id.result_text_view)).setText(res);
}
}
You should consider calling super.onSaveInstanceState()
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
// save your state
super.onSaveInstanceState(outState);
}
Also, to avoid recreating the activity everytime the screen is rotated you could handle config change yourself by adding this to your Activity declaration in the app manifest :
android:configChanges="keyboardHidden|orientation|screenSize"
And override :
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
It could save time for the user if your app doesn't need to be recreated after orientation or screenSize changes...
Are you sure values are not retrieved?
I believe the issue here is that you always call setActual() in onCreate() which generates x and y values and assign z value to the generated x and y (even when savedInstanceState != null)
better way to handle things is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = findViewById(R.id.editText);
textBox.requestFocus();
if (savedInstanceState != null) {
x = savedInstanceState.getInt("x");
y = savedInstanceState.getInt("y");
score = savedInstanceState.getInt("score");
z = x * y;
String hintStr = x + " × " + y;
((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
} else { setActual(); }
}
You should also call super.onSaveInstanceState(savedInstanceState) like that
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("x", x);
savedInstanceState.putInt("y", y);
savedInstanceState.putInt("score", score);
}

Android Quiz app crashing while updating score

The submit button id is ShowScore
correct answers are right1,right2...right53
when the button is clicked the app crashes...
android studios doest show any errors
right answer awards 1 point
wrong answer is -1
please help me out
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setScore() {
RadioButton check1 = (RadioButton) findViewById(R.id.right1);
boolean doit1 = check1.isChecked();
RadioButton check2 = (RadioButton) findViewById(R.id.right2);
boolean doit2 = check2.isChecked();
RadioButton check3 = (RadioButton) findViewById(R.id.right3);
boolean doit3 = check3.isChecked();
RadioButton check4 = (RadioButton) findViewById(R.id.right4);
boolean doit4 = check4.isChecked();
CheckBox check5 = (CheckBox) findViewById(R.id.right51);
boolean doit5 = check5.isChecked();
CheckBox check52 = (CheckBox) findViewById(R.id.right52);
boolean doit52 = check52.isChecked();
CheckBox check53 = (CheckBox) findViewById(R.id.right53);
boolean doit53 = check53.isChecked();
updateScore(doit1);
updateScore2(doit2);
updateScore3(doit3);
updateScore4(doit4);
updateScore5(doit5, doit52, doit53);
showScore();
}
private int updateScore(boolean doit1) {
if (doit1) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore2(boolean doit2) {
if (doit2) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore3(boolean doit3) {
if (doit3) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore4(boolean doit4) {
if (doit4) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore5(boolean doit5, boolean doit52, boolean doit53) {
if (doit5 && doit52 && doit53) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private void showScore() {
TextView olds = (TextView) findViewById(R.id.ShowScore);
olds.setText(score);
}
}
#. I guess you have defined setScore() method to SUBMIT Button in your XML using attribute android:onClick="setScore". But your method doesn't have any View parameter. Update your method as below:
public void setScore(View v) {
..........
.............
}
#. As score is an int value, use olds.setText(String.valueOf(score)) to set score on TextView.
Here is the full code:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView olds;
RadioButton check1, check2, check3, check4;
CheckBox check5, check52, check53;
int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
olds = (TextView) findViewById(R.id.ShowScore);
check1 = (RadioButton) findViewById(R.id.right1);
check2 = (RadioButton) findViewById(R.id.right2);
check3 = (RadioButton) findViewById(R.id.right3);
check4 = (RadioButton) findViewById(R.id.right4);
check5 = (CheckBox) findViewById(R.id.right51);
check52 = (CheckBox) findViewById(R.id.right52);
check53 = (CheckBox) findViewById(R.id.right53);
}
public void setScore(View v) {
boolean doit1 = check1.isChecked();
boolean doit2 = check2.isChecked();
boolean doit3 = check3.isChecked();
boolean doit4 = check4.isChecked();
boolean doit5 = check5.isChecked();
boolean doit52 = check52.isChecked();
boolean doit53 = check53.isChecked();
updateScore(doit1);
updateScore2(doit2);
updateScore3(doit3);
updateScore4(doit4);
updateScore5(doit5, doit52, doit53);
showScore();
}
private int updateScore(boolean doit1) {
if (doit1) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore2(boolean doit2) {
if (doit2) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore3(boolean doit3) {
if (doit3) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore4(boolean doit4) {
if (doit4) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private int updateScore5(boolean doit5, boolean doit52, boolean doit53) {
if (doit5 && doit52 && doit53) {
score = score + 1;
} else {
score = score - 1;
}
return score;
}
private void showScore() {
olds.setText(String.valueOf(score));
}
}
Change setText.(score) to (score+"");
Explanation: setText method accepts only String as an argument but yes you can pass in an int but it will give an error and I suppose that output stream in android platform must always be String type.
You can even use String.valueOf(score); since this is the right way and adding quotation marks set in the first paramater is like a geek cheat. If you have set onClick method in xml, then setScore() must take the parameter setScore (View view) { //text view goes here..}
Hope it works. Cheers!

Android Java : Error while running countdown timer

I need your help to find an error in my "test" android application.
Until I create the second function "setProgessBar" the app runs without error, but now the error occur everytime. To comment the function call out has no effect.
Here is the code:
package my.timer;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Timer1 extends AppCompatActivity {
private int arrayPosition = 0;
private long timeValue = 0;
private boolean indicator = false;
private double progress;
private final String[] actions = {"Phase1" , "Phase2" , "Phase3" , "Phase4"};
private final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer1);
final Button startButton = (Button) findViewById(R.id.startButton);
final TextView text1 = (TextView) findViewById(R.id.textView3);
final TextView text2 = (TextView) findViewById(R.id.actionParameter);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text2.setText(actions[arrayPosition]);
text1.setText("" + timeValue + "");
new CountDownTimer(240000, 100) {
public void onTick(long millisUntilFinished) {
timeValue = (millisUntilFinished / 1000) % 4;
text1.setText("" + timeValue + "");
if (timeValue == 3 && indicator){
if (arrayPosition < 3){
arrayPosition++;
} else {
arrayPosition = 0;
}
indicator = false;
setProgressBar(arrayPosition);
text2.setText(actions[arrayPosition]);
}
if (timeValue == 1){
indicator = true;
}
}
public void onFinish() {
text1.setText("Geschafft :)");
}
}.start();
}
});
}
private void setProgressBar(int progressStatus) {
switch (progressStatus){
case 0:
progress = progress + 0.25;
break;
case 2:
progress = progress - 0.25;
break;
}
progressBar.setProgress((int)progress);
}
}
Many thanks in advance
Tim

How to put a number decimal (###,###,###.00) in a string value

Hello all, I would like my number (resultat) seems like this ###,###,###.00. Into (resultat) in the String.valueOf from the below phrase of the below Java Code. Thanks for your answers.
result.setText("the rent is " + String.valueOf(resultat) + " currency");
package albencreation.realestateapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Rent extends AppCompatActivity {
EditText price = null;
EditText profit = null;
TextView result = null;
Button envoyer = null;
Button close = null;
Button info = null;
Button clear = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rent);
price = (EditText) findViewById(R.id.price);
profit = (EditText) findViewById(R.id.profit);
result = (TextView) findViewById(R.id.result);
envoyer = (Button) findViewById(R.id.buttcalculate);
close = (Button) findViewById(R.id.buttclose);
info = (Button) findViewById(R.id.buttinfo);
clear = (Button) findViewById(R.id.buttclear);
envoyer.setOnClickListener(envoyerListener);
close.setOnClickListener(closeListener);
info.setOnClickListener(infoListener);
clear.setOnClickListener(clearListener);
}
private OnClickListener envoyerListener = new OnClickListener() {
#Override
public void onClick(View v) {
String p = price.getText().toString();
String o = profit.getText().toString();
float pValue;
if (p.isEmpty()) {
pValue = 0;
} else {
pValue = Float.valueOf(p);
}
float oValue;
if (o.isEmpty()) {
oValue = 0;
} else {
oValue = Float.valueOf(o);
}
float resultat = oValue * pValue / 100;
result.setText("the rent is " + String.valueOf(resultat) + " currency");
}
};
private OnClickListener closeListener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent jumpage = new Intent(Rent.this, MainActivity.class);
startActivity(jumpage);
}
};
private OnClickListener infoListener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent jumpage = new Intent(Rent.this, Inforent.class);
startActivity(jumpage);
}
};
private OnClickListener clearListener = new OnClickListener() {
#Override
public void onClick(View v) {
price.getText().clear();
profit.getText().clear();
String defaut = "result rent";
result.setText(defaut);
}
};
}
Use DecimalFormat to format the string as you like. For integer values use # and for decimal places use 0
DecimalFormat formatter = new DecimalFormat("###,###,###.00");
String resultString = formatter.format(resultat);
result.setText("the rent is " + resultString + " currency");
try this :
import java.text.DecimalFormat;
float f = YourValue;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(f));
This code for show Two Dights Hope will help you

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);
//--------------------------------------------------------------
*/
}
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();
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);

Categories