题目
1.完成一个秒表,具备启停功能,正确使用工作线程完成界面刷新
分析
MainActivity中定义刷新界面函数UpdateGUI(),后台service负责系统时间读取。
使用Header更新用户界面,将后台线程产生的数据传递到updateGUI()函数内部,直接调用post()方法,service服务用来获取系统时间间隔,刷新界面。
结果
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static Handler handler = new Handler();
static long spacing_time;
static TextView hour;//分钟
static TextView minute;//秒
static TextView second;//微秒-10微秒为单位
/**
* j界面刷新函数
* @param refreshlong 间隔时间
*/
public static void UpdateGUI(long refreshlong){
spacing_time=refreshlong;//获取间隔时间
handler.post(RefreshLable);
}
private static Runnable RefreshLable = new Runnable() {
@Override
public void run() {
if (spacing_time<6000){
if (spacing_time<100)
second.setText(Long.toString(spacing_time));
else {
second.setText(Long.toString(spacing_time%100));
minute.setText(Long.toString(spacing_time/100));
}
}else {
hour.setText(Long.toString(spacing_time/6000));
minute.setText(Long.toString((spacing_time%6000)/100));
second.setText(Long.toString((spacing_time%6000)%100));
}
}
};
//初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hour = (TextView)findViewById(R.id.hour);
minute = (TextView)findViewById(R.id.minute);
second = (TextView)findViewById(R.id.second);
Button clear = (Button)findViewById(R.id.clear);
Button timing = (Button)findViewById(R.id.timing);
Button stop = (Button)findViewById(R.id.stop);
//服务显示启动service
final Intent serviceIntent = new Intent(this,MainService.class);
//重写点击函数
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(serviceIntent);
hour.setText("00");
minute.setText("00");
second.setText("00");
}
});
timing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(serviceIntent);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(serviceIntent);
}
});
}
}
MainService.java
public class MainService extends Service {
private Thread workthread;
private long start_time;
private long stop_time;
public MainService() {
}
//绑定函数
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
//每过10微秒刷新界面
private Runnable backgroundWork = new Runnable() {
@Override
public void run() {
try {
while (!workthread.interrupted()){
stop_time = (System.currentTimeMillis()-start_time)/10;
MainActivity.UpdateGUI(stop_time);
Thread.sleep(10);
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
@Override
public void onCreate() {
super.onCreate();
workthread = new Thread(null,backgroundWork,"workthread");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if(!workthread.isAlive()){
workthread.start();//启动线程
start_time = System.currentTimeMillis();//读取系统时间
}
}
@Override
public void onDestroy() {
super.onDestroy();
workthread.interrupt();//准备终止
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="秒表">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<TextView
android:id="@+id/hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":">
</TextView>
<TextView
android:id="@+id/minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":">
</TextView>
<TextView
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清零">
</Button>
<Button
android:id="@+id/timing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计时">
</Button>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止">
</Button>
</LinearLayout>
</LinearLayout>