why thread don't work accurately (a count down with thread) - android

I need count down timer that whenever I clicked or times up, send result and time score to another activity. So I used thread, but I don't know why in first time run activity it don't show time for some seconds in textview, one more strange thing is that when press back,then go to activity again it works fine.
count down code:
if (flagTime) {
flagTime = false;
new Thread(new Runnable() {
#Override
public void run() {
int counter = 60;
while (function.isActivityVisible() && counter > 0) {
counter--;
final int finalCounter = counter;
try {
Thread.sleep(1000);
G.HANDLER.post(new Runnable() {
#Override
public void run() {
if (function.isActivityVisible()) {
timeSc=finalCounter;
String preSec = "";
if (finalCounter < 10) {
preSec = "0";
}
if (function.isActivityVisible()
&& finalCounter <= 0) {
flagTime = false;
Intent intent = new Intent(
Memorize.this,
MemorizeResult.class);
intent.putExtra("TIME", 60);
intent.putExtra("ARRAY_RESULT",
array_choice);
startActivity(intent);
finish();
}
String score = "00:" + preSec
+ finalCounter;
txt[20].setText( " time ramaining " + score);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
if (!function.isActivityVisible()) {
flagTime = false;
finish();
}
}
}).start();
}
I use many flag to handle it, but it seems don't work properly.

Try using a CountDownTimer
Something like:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Related

How to use intervals in Android CountdownTimer

I am creating a countdown time in Android and I have the timer part working perfectly. However, I want to add a feature where the user can set the number of times they wish the timer to repeat. This is my current attempt at implementation
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
minutesText = (EditText) findViewById(R.id.minutesText);
secondsText = (EditText) findViewById(R.id.secondsText);
startButton = (Button) findViewById(R.id.startButton);
intervalCount = (EditText) findViewById(R.id.intervalCount);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (minutesText.getText().toString().equals("")) {
minutesInt = 0;
} else {
minutesString = minutesText.getText().toString();
minutesInt = Integer.parseInt(minutesString);
}
secondsString = secondsText.getText().toString();
if (secondsText.getText().toString().equals("")) {
secondsInt = 0;
} else {
secondsString = secondsText.getText().toString();
secondsInt = Integer.parseInt(secondsString);
}
intervalsString = intervalCount.getText().toString();
intervals = Integer.parseInt(intervalsString);
final int timerAmount = ((minutesInt * 60) + (secondsInt)) * 1000;
CountDownTimer timer = new CountDownTimer(timerAmount, 1000) {
#RequiresApi(api = Build.VERSION_CODES.N)
public void onTick(long millisUntilFinished) {
isRunning = true;
String timeLeft = String.format("%02d : %02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))
);
textView.setText("Reamining: " + timeLeft);
}
public void onFinish() {
isRunning = false;
intervals--;
if (intervals > 0) {
timer.start();
}
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
};timer.start();
}
});
My error is in the onFinish()
it wont let me use timer.start()
CountDownTimer basically uses a Handler thread which is like other threads can not be restarted once its ended.
You need to use a new instance of the CountDownTimer in each interval.
So in your case, bring the
intervals--;
if (intervals > 0) {
// for each interval left create a new CountDownTimer here and start it.
}
outside of
new CountDownTimer(timerAmount, 1000 } {
// do your operations here.
}

Android - Timer layout is not getting updated

I am new to android. I am learning to create a simple stop watch app. I got layout with three buttons and one textview. When I click start button, it will start the timer.
Single layout and single activity.
public void startTimer(View view){
running = true;
TextView textView = (TextView) findViewById(R.id.timer);
while(running) {
int hours = seconds / 3600;
int minutes = (seconds % 60) / 60;
int sec = seconds % 60;
String time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
textView.setText(time);
seconds++;
if(seconds == 10){
running = false;
}
}
}
This is the method called when I click start button. I debugged the code. Values are generating properly. But not updating the layout.
Any suggestions?
I get the final result else one by one increase with the following addition
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_watch);
runTimer(); //I added this extra.
}
I will give you an alternate solution on your task,
public void startTimer(View view){
new CountDownTimer(10000, 1000) { //For 10 seconds
public void onTick(long seconds) {
String time = String.format("%02d : %02d ",
TimeUnit.MILLISECONDS.toMinutes(seconds),
TimeUnit.MILLISECONDS.toSeconds(seconds) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(seconds))
);
textView.setText(time);
}
}
public void onFinish() {
textView.setText("Finished");
}
}.start();
}
UPDATE :
public void startTimer(View view) {
running = true;
seconds = 0;
textView = (TextView) findViewById(R.id.txtView);
new Thread(new Runnable() {
#Override
public void run() {
while(running) {
runOnUiThread(new Runnable() {
#Override
public void run() {
int hours = seconds / 3600;
int minutes = (seconds % 60) / 60;
int sec = seconds % 60;
time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
textView.setText(time);
seconds++;
}
}) ;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(seconds==10){
running=false;
}
}
}).start();
}
Try using below code
public void startTimer(View view){
running = true;
TextView textView = (TextView) findViewById(R.id.timer);
while(running) {
int hours = seconds / 3600;
int minutes = (seconds % 60) / 60;
int sec = seconds % 60;
String time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
seconds++;
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(time);
}
});
Thread.sleep(1000)
if(seconds == 10){
running = false;
}
}
}

How to know when scroll animation is finished?

I do a smooth scroll to defined position.
smoothScrollTo(0, 1000);
After this method execute - i do a couple operations. But i want to do they - after scroll animation end.
I want to detect when scroll animation is finished. How i can do that?
i have do like this,
final ScrollView mScrollView = (ScrollView) findViewById(R.id.scrollview);
layoutSponsorBax.setVisibility(View.VISIBLE);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
ViewGroup relativeL = (ViewGroup) mScrollView
.getChildAt(mScrollView.getChildCount() - 1);
final int sVH = mScrollView.getBottom()
- mScrollView.getTop();
View notePad = relativeL.getChildAt(2);
final int svHeight = 800;
Log.i("svHeight", "svHeight : " + svHeight);
final float duration = 2000;
ctt = new CountDownTimer((int) duration, 1) {
int temp = 0;
public void onTick(long millisUntilFinished) {
try {
Log.i("millisUntilFinished",
"millisUntilFinished : "
+ millisUntilFinished);
Log.i("Height- Temp", "Temp : " + temp);
mScrollView.scrollTo(0, temp);
temp = (int) (((svHeight) - (millisUntilFinished / duration)
* (svHeight)));
} catch (ArithmeticException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onFinish() {
}
}.start();
}
});
}
};
int duration = 1400;
timer.schedule(task, duration);
please check this it may be help to you

Why doesn't my CountDownTimer is not count?

I have a CountDownTimer in for loop, but it doesn't count. Why?
for (int i = 0; i < payloadList.size(); i++) {
sonpx.dtnsocialnetwork.Bundle bundle = new sonpx.dtnsocialnetwork.Bundle(
srcIMEI, desIMEI, fileName, i, totalFragment,
numOfSendingRest, payloadList.get(i));
byte[][] payload = new byte[1][];
try {
payload[0] = serialize(bundle);
} catch (IOException e) {
e.printStackTrace();
}
Log.i(TAG, "bundle payload size: "+payload[0].length);
mDefaultChannel.sendData(nodeName, DIRECT_BUNDLE, payload);
CountDownTimer countDownTimer = new CountDownTimer(5000, 100) {
#Override
public void onTick(long millisUntilFinished) {
if (bundleResultOk) {
cancel();
bundleResultOk = false;
}
Log.i(TAG, "bundle result: "+bundleResultOk);
}
#Override
public void onFinish() {
counterFinish = true;
Log.i(TAG, "Send Message time out");
}
};
countDownTimer.start();
}
It doesn't print Log.i(TAG, "bundle result: "+bundleResultOk);
maybe the code in not running in a HandlerThread
Use this custom CountdownTimer

How to display a progress bar for 1 minute?

I have a horizontal progress bar in android. I need to make it complete in 60 seconds.
Why doesn't the below code work?
int progress = 0;
progressBarHorizontal.setMax(60);
while(progress < 60) {
progressHandler.postDelayed((new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
}),progress * 1000);
progress++;
}
Please suggest some other methods. I tried this:
new Thread(new Runnable() {
int progress = 0;
public void run() {
long timerEnd = System.currentTimeMillis() + 60 * 1000;
while (timerEnd > System.currentTimeMillis() ) {
progress = (int) (timerEnd - System.currentTimeMillis()) / 1000;
// Update the progress bar
progressHandler.post(new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.w("tag","Progress thread cannot sleep");
}
}
}
}).start();
But it is not working either.
The second piece of code actually works, the problem was with my logic.
you can try the CountDownTimer :
pd = ProgressDialog.show(MovementEntry.this, "", "Please Wait...",
true, false);
pd.show();
new CountDownTimer(60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//this will be done every 1000 milliseconds ( 1 seconds )
int progress = (60000 - millisUntilFinished) / 1000;
pd.setProgress(progress);
}
#Override
public void onFinish() {
//the progressBar will be invisible after 60 000 miliseconds ( 1 minute)
pd.dismiss();
}
}.start();
This one will update the statusbar until it reaches its maximum value:
private int progress = 0;
private final int pBarMax = 60;
...
final ProgressBar pBar = (ProgressBar) findViewById(R.id.progressBar1);
pBar.setMax(pBarMax);
final Thread pBarThread = new Thread() {
#Override
public void run() {
try {
while(progress<=pBarMax) {
pBar.setProgress(progress);
sleep(1000);
++progress;
}
}
catch(InterruptedException e) {
}
}
};
pBarThread.start();
You can try this code for that:
Thread timer = new Thread(){
public void run(){
try{
sleep(10000);
while(progressBarStatus < 10000){
StartPoint.this.runOnUIThread(new Runnable(){
public void run()
{
progressBar.setProgress(progressBarStatus);
progressBarStatus += 1000;
}
});
}
}catch(InterruptedException e){
e.printStackTrace();
}finally{
}
}
};
timer.start();
This code works for me
new Thread(new Runnable() {
public void run() {
long timerEnd = System.currentTimeMillis() + 60 * 1000;
while (timerEnd > System.currentTimeMillis()) {
progress = 60 - (int) (timerEnd - System.currentTimeMillis()) / 1000;
// Update the progress bar
progressHandler.post(new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.w("App","Progress thread cannot sleep");
}
}
progressHandler.post(new Runnable() {
public void run() {
okButton.performClick();
}
});
}
}).start();

Categories

Resources