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();
}
}
}
AnalogClock时钟
最新推荐文章于 2022-08-22 08:35:47 发布