New developer - basic java question involving Timer implementation
I'm learning Android and java while building a timer application for myself.
Referencing an old thread, Android - Controlling a task with Timer and TimerTask? I am trying to create the Runnable method to count down my timer. The basic java issue I'm stuck on is what to class do I attach the postDelayed() call?My activity is called TimerButtons for now, and I thought this would work:
package com.TimerButtons;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TimerButtons extends Activity {
private TextView mDisplayTime;
private Button mButtonStart;
private Button mButtonStop;
private int timeTenths = 0;
private Drawable d;
private PorterDuffColorFilter filter;
// capture our View elements
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
filter = new PorterDuffColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_ATOP);
d = findViewById(R.id.buttonStart).getBackground(); d.setColorFilter(filter);
d = findViewById(R.id.buttonStop).getBackground(); d.setColorFilter(filter);
mDisplayTime = (TextView) findViewById(R.id.displayTime);
mButtonStart = (Button) findViewById(R.id.buttonStart);
mB开发者_开发问答uttonStop = (Button) findViewById(R.id.buttonStop);
// add click listeners to the buttons
mButtonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Thread(r).start();
}
});
mButtonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
timeTenths = 0;
updateDisplay();
updateSetting();
}
});
// display the current time
updateDisplay();
}
// runtime methods below here
// updates the time we display in the TextView
private void updateDisplay() {
mDisplayTime.setText(
String.valueOf(((float)timeTenths)/10)
);
}
private void updateSetting() {
mTensDigit.setText(String.valueOf(timeTenths/100));
mOnesDigit.setText(String.valueOf((timeTenths%100)/10));
mTenthsDigit.setText(String.valueOf(timeTenths%10));
}
Runnable r = new Runnable()
{
public void run()
{
if (timeTenths >= 1)
{
timeTenths -= 1;
if (timeTenths != 0)
mDisplayTime.postDelayed(this, 100);
updateDisplay();
}
}
};
}
I get the error: The method postDelayed(new Runnable(){}, int) is undefined for the type TimerButtons on the commented line.
Thanks for any noob guidance!
DaveIf TimerButtons is your Activity, that should work. Try this:
TimerButtons.this.postDelayed(this, 1000);
EDIT (for posterity): I was initially wrong: postDelayed
is defined on a View
, not a Context
. Use
mDisplayTime.postDelayed(this, 1000);
I think you have the handler class to complete your task.
you have approach it differently...
I don't know what you are really want to do with this class but this is one implementation.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import com.skens.sms.R;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TimerButtons extends Activity {
private TextView mDisplayTime;
private Button mButtonStart;
private Button mButtonStop;
//private int timeTenths = 0;
private Drawable d;
private PorterDuffColorFilter filter;
private TinyTimer myTimer;
// capture our View elements
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
filter = new PorterDuffColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_ATOP);
d = findViewById(R.id.buttonStart).getBackground(); d.setColorFilter(filter);
d = findViewById(R.id.buttonStop).getBackground(); d.setColorFilter(filter);
mDisplayTime = (TextView) findViewById(R.id.displayTime);
mButtonStart = (Button) findViewById(R.id.buttonStart);
mButtonStop = (Button) findViewById(R.id.buttonStop);
// add click listeners to the buttons
mButtonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//new Thread(r).start();
myTimer.startTimer(1000, mDisplayTime, mTensDigit, mOnesDigit, mTenthsDigit);
}
});
mButtonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//timeTenths = 0;
//updateDisplay();
updateSetting();
}
});
myTimer = new TinyTimer();
// display the current time
//updateDisplay();
}
// runtime methods below here
// updates the time we display in the TextView
private void updateDisplay() {
//mDisplayTime.setText(
// String.valueOf(((float)timeTenths)/10)
//);
}
private void updateSetting() {
//mTensDigit.setText(String.valueOf(timeTenths/100));
//mOnesDigit.setText(String.valueOf((timeTenths%100)/10));
//mTenthsDigit.setText(String.valueOf(timeTenths%10));
}
//Runnable r = new Runnable()
//{
// public void run()
// {
// if (timeTenths >= 1)
// {
// timeTenths -= 1;
// if (timeTenths != 0)
// mDisplayTime.postDelayed(this, 100);
// updateDisplay();
// }
// }
//};
public class TinyTimer {
private Handler _handler;
private long _startTime = 0;
private long _millis;
private long _delayMillis;
private TextView mDisplayTime;
private TextView mTensDigit;
private TextView mOnesDigit;
private TextView mTenthsDigit;
private String _format = String.format("%%0%dd", 2);
private int timeTenths = 0;
public TinyTimer(){
_handler = new Handler();
}
public void startTimer
(long delayMillis,TextView DisplayTime, TextView TensDigit, TextView OnesDigit, TextView TenthsDigit){
mDisplayTime = (TextView) DisplayTime;
mTensDigit = (TextView) TensDigit;
mOnesDigit = (TextView) OnesDigit;
mTenthsDigit = (TextView) TenthsDigit;
_delayMillis = delayMillis;
_startTime = SystemClock.elapsedRealtime();
_handler.removeCallbacks(updateTimerTask);
updateTimerTask.run();
}
public void stopTimer(){
_handler.removeCallbacks(updateTimerTask);
}
public void pauseTimer(){
_handler.removeCallbacks(updateTimerTask);
}
public void unpauseTimer(){
if(_startTime != 0){
updateTimerTask.run();
}
}
private Runnable updateTimerTask = new Runnable() {
@Override
public void run() {
final long start = _startTime;
_millis = SystemClock.elapsedRealtime() - start;
if (timeTenths >= 1)
{
timeTenths -= 1;
if (timeTenths != 0)
mDisplayTime.postDelayed(this, 100);
mDisplayTime.setText(String.valueOf(((float)timeTenths)/10));
mTensDigit.setText(String.valueOf(timeTenths/100));
mOnesDigit.setText(String.valueOf((timeTenths%100)/10));
mTenthsDigit.setText(String.valueOf(timeTenths%10));
}
_handler.postDelayed(updateTimerTask, _delayMillis);
}
};
public long getElapsedtime(){
return _millis;
}
public String getMillis(){
return String.format(_format, _millis % 1000);
}
}
The class i implemented is just a concept. I haven't test but compile class.
精彩评论