熟悉和掌握Android线程的使用
1. 完成一个秒表,具备启停功能,正确使用工作线程完成界面刷新
2. 分析秒表的计时是否准确,原因是什么,如何保证秒表计时准确
package com.walker.exp5;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
private static Handler handler = new Handler();
private Button btn_clear;
private Button btn_start;
private Button btn_stop;
private static TextView tv_hh;
private static TextView tv_mm;
private static TextView tv_ss;
public static int hh;
public static int mm;
public static int ss;
public static void UpadteGUI(int h, int m, int s) {
hh = h;
mm = m;
ss = s;
handler.post(RefreshLable);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_clear = findViewById(R.id.btn_clear);
btn_start = findViewById(R.id.btn_start);
btn_stop = findViewById(R.id.btn_stop);
tv_hh = findViewById(R.id.tv_hh);
tv_mm = findViewById(R.id.tv_mm);
tv_ss = findViewById(R.id.tv_ss);
final Intent intent = new Intent(this, MainService.class);
//start
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// String s = tv_ss.getText().toString();
// tv_ss.setText(s);
//Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
startService(intent);
Toast.makeText(MainActivity.this,"开始计时",Toast.LENGTH_SHORT).show();
}
});
// /clear
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv_hh.setText("00");
tv_mm.setText("00");
tv_ss.setText("00");
stopService(intent);
Toast.makeText(MainActivity.this,"清零",Toast.LENGTH_SHORT).show();
}
});
//stop
btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(intent);
Toast.makeText(MainActivity.this,"暂停计时",Toast.LENGTH_SHORT).show();
}
});
}
private static Runnable RefreshLable = new Runnable() {
@Override
public void run() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumIntegerDigits(2);
tv_hh.setText(nf.format(hh));
tv_mm.setText(nf.format(mm));
tv_ss.setText(nf.format(ss));
}
};
// public class MyThread implements Runnable {
// int h = Integer.parseInt(tv_hh.getText().toString());
// int m = Integer.parseInt(tv_mm.getText().toString());
// int s = Integer.parseInt(tv_ss.getText().toString());
//
// @Override
// public void run() {
// while (true) {
// try {
// Thread.sleep(1000);
// } catch (Exception e) {
// e.printStackTrace();
// break;
// }
// s++;
// if (s >= 60) {
// m++;
// s = 0; //秒钟等于60,分钟加1,秒钟置0
// if (m >= 60) {
// h++;
// m = 0;
// if (h >= 24) {
// h = 0;
// }
// }
// }
// tv_hh.setText(String.valueOf(h));
// tv_mm.setText(String.valueOf(m));
// tv_ss.setText(String.valueOf(s));
//
// }
//
// }
// }
// public void clear(View v) {
// tv_hh.setText("00");
// tv_mm.setText("00");
// tv_ss.setText("00");
// stopService(new Intent(MainActivity.this, MainService.class));
// Toast.makeText(this, "clear", Toast.LENGTH_SHORT).show();
//
// }
//
// public void start(View v) {
// //thread = new Thread(new MyThread()); // start thread
// //thread.start();
// startService(new Intent(MainActivity.this, MainService.class));
// Toast.makeText(this, "start", Toast.LENGTH_SHORT).show();
//
// }
//
// public void stop(View v) {
// Toast.makeText(this, "stop", Toast.LENGTH_SHORT).show();
// thread.interrupt();
// //thread.run();
// //stopService(new Intent(MainActivity.this, MainService.class));
// }
}
package com.walker.exp5;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainService extends Service {
private Thread workthread;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
workthread = new Thread(null,backgroudWork,"workThread");
//Log.e("ATG","onCreate");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if(!workthread.isAlive()){
workthread.start();
}
// Toast.makeText(this,"onStart()",Toast.LENGTH_SHORT).show();
//Log.e("ATG","onStartCommand");
}
// @Override
// public int onStartCommand(Intent intent, int flags, int startId) {
// if(!workthread.isAlive()){
// workthread.start();
// }
// Log.e("ATG","onStartCommand");
// return super.onStartCommand(intent, flags, startId);
//
// }
@Override
public void onDestroy() {
//super.onDestroy();
workthread.interrupt();
// Log.e("ATG","onDestroy");
//Toast.makeText(this,"onDestroy()",Toast.LENGTH_SHORT).show();
}
private Runnable backgroudWork = new Runnable() {
@Override
public void run() {
LayoutInflater layoutInflater = LayoutInflater.from(MainService.this);
View view = layoutInflater.inflate(R.layout.activity_main,null);
TextView tv_hh = view.findViewById(R.id.tv_hh);
TextView tv_mm = view.findViewById(R.id.tv_mm);
TextView tv_ss = view.findViewById(R.id.tv_ss);
int h = Integer.parseInt(tv_hh.getText().toString());
int m = Integer.parseInt(tv_mm.getText().toString());
int s = Integer.parseInt(tv_ss.getText().toString());
while (true) {
try {
Thread.sleep(1000);
s++;
if (s >= 60) {
m++;
s = 0; //秒钟等于60,分钟加1,秒钟置0
if (m >= 60) {
h++;
m = 0;
if (h >= 24) {
h = 0;
}
}
}
MainActivity.UpadteGUI(h,m,s);
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
};
}
activity_main.xml