您的位置:首页 > 其它

AnalogClock时钟

2016-04-21 09:23 246 查看
public class MainActivity extends AppCompatActivity {

protected static final int GUINOTIFIER=0x1234;

private TextView myTextView;
private AnalogClock myAnalogClock;

private Calendar mCalendar;
private int mMinutes;
private int mHour;

private Handler mHandler;
private Thread mClockThread;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myTextView=(TextView)findViewById(R.id.myTextView);
myAnalogClock=(AnalogClock)findViewById(R.id.myAnalogClock);

mHandler=new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what)
{
case MainActivity.GUINOTIFIER:
myTextView.setText(mHour+":"+mMinutes);
break;
}

super.handleMessage(msg);
}
};

mClockThread=new LooperThread();
mClockThread.start();
}

class LooperThread extends Thread
{
public void run()
{
super.run();
try
{
do
{
long time=System.currentTimeMillis();
final Calendar mCalender=Calendar.getInstance();
mCalender.setTimeInMillis(time);
mHour=mCalender.get(Calendar.HOUR);
mMinutes=mCalender.get(Calendar.MINUTE);

//Thread.sleep(1000);

Message m=new Message();
m.what=MainActivity.GUINOTIFIER;
MainActivity.this.mHandler.sendMessage(m);
}
while (MainActivity.LooperThread.interrupted()==false);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: