Simple Android Binary Text Clock
Posted
by Hristo
on Stack Overflow
See other posts from Stack Overflow
or by Hristo
Published on 2010-05-13T22:18:08Z
Indexed on
2010/05/14
2:34 UTC
Read the original article
Hit count: 534
Hello,
I want to create a simple android binary clock but my application crashes. I use 6 textview fields: 3 for the decimal and 3 for the binary representation of the current time (HH:mm:ss). Here's the code:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Binary extends Activity implements Runnable
{
Thread runner;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (runner == null)
{ //start the song
runner = new Thread(this);
runner.start();
}
}
@Override
public void run()
{
TextView hours_dec = (TextView) findViewById(R.id.hours_dec);
TextView mins_dec = (TextView) findViewById(R.id.mins_dec);
TextView secs_dec = (TextView) findViewById(R.id.secs_dec);
TextView hours_bin = (TextView) findViewById(R.id.hours_bin);
TextView mins_bin = (TextView) findViewById(R.id.mins_bin);
TextView secs_bin = (TextView) findViewById(R.id.secs_bin);
SimpleDateFormat hours_sdf = new SimpleDateFormat("HH");
SimpleDateFormat mins_sdf = new SimpleDateFormat("mm");
SimpleDateFormat secs_sdf = new SimpleDateFormat("ss");
Calendar cal = Calendar.getInstance();
while (runner != null)
{
WaitAMoment();
cal.getTime();
hours_dec.setText(hours_sdf.format(cal.getTime()));
mins_dec.setText(mins_sdf.format(cal.getTime()));
secs_dec.setText(secs_sdf.format(cal.getTime()));
hours_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) hours_dec.getText()))));
mins_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) mins_dec.getText()))));
secs_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) secs_dec.getText()))));
}
}
protected void WaitAMoment()
{
try
{
Thread.sleep(100);
} catch (InterruptedException e) { };
}
}`
© Stack Overflow or respective owner