半期考试 之 多线程
Posted 春招进大厂的梦想家
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了半期考试 之 多线程相关的知识,希望对你有一定的参考价值。
半期考试 之 多线程1
要求:
- 实现一个计时器,点击“启动”按钮后,计时器开始运行;
- 点击“记录”按钮,下方textView记录当前计时器的时长;
- 点击“定时器”按钮,启动一个三秒钟的计时器,三秒后TextView显示“三秒钟到了”
- 要求用android 多线程来实现
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/activity_main_tvText"
android:layout_width="match_parent"
android:layout_height="64dp"
android:gravity="center_horizontal|center_vertical"
android:text="TextView"
android:textSize="20dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/activity_main_bnStartOrStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="启动" />
<Button
android:id="@+id/activity_main_bnRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="记录" />
<Button
android:id="@+id/activity_main_bnTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="定时器" />
</LinearLayout>
<TextView
android:id="@+id/activity_main_tvResult"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:text="0:0.7"
android:textSize="18dp" />
</LinearLayout>
java
public class MainActivity extends AppCompatActivity implements View.OnClickListener
//布局中的UI组件
private TextView tvText;
private TextView tvRecord;
private Button bnStartOrStop;
private Button bnRecord;
private Button bnTimer;
private Button bnAsyncTask;
//定义时间:分钟,秒,厘秒
private int Minute = 0;
private int Second = 0;
private int CentiSecond = 0;
//定义Handler对象接收处理UI线程相关数据和组件
final Handler handler = new Handler()
@Override
public void handleMessage(Message msg)
// TODO Auto-generated method stub
super.handleMessage(msg);
//获取传递过来的Bundle数据
Bundle bundle = msg.getData();
int Counter = bundle.getInt("Counter", 0);
//根据msg.what判断处理什么类型的数据
switch (msg.what)
case 1:
Minute = Counter / 6000;
Second = Counter % 6000 / 100;
CentiSecond = Counter % 6000 % 100;
tvText.setText(String.format("%02d:%02d.%02d", Minute, Second, CentiSecond));
break;
case 3:
tvText.setText("定时3秒我成功的启动了哦");
break;
;
//定义多线程内部类实现一个秒表的数据更新
private class StopwatchThread extends Thread
//标志位:启动停止线程运行:通过标志位来结束线程运行能保证数据的完整性
private boolean isFlag = false;
private Bundle data;
private Message msg;
public StopwatchThread()
isFlag = true;
data = new Bundle();
@Override
public void run()
int Counter = 0;
while (isFlag)
try
data.putInt("Counter", Counter);
msg = new Message();
//指定从哪一个线程向UI线程发送消息
msg.what = 1;
msg.setData(data);
//向handle对象发送消息
handler.sendMessage(msg);
//休眠
Thread.sleep(10);
//计数器加1
Counter++;
catch (Exception ex)
//此方法能保证run里面的代码执行完成
public void Stop()
isFlag = false;
//定义Timer对象
private final Timer timer = new Timer();
//定义TimerTask的引用变量
private TimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//关联布局文件中的组件并设置相应按钮的监听事件
tvText = findViewById(R.id.activity_main_tvText);
tvRecord = findViewById(R.id.activity_main_tvResult);
bnStartOrStop = findViewById(R.id.activity_main_bnStartOrStop);
bnStartOrStop.setOnClickListener(this);
bnRecord = findViewById(R.id.activity_main_bnRecord);
bnRecord.setOnClickListener(this);
bnTimer = findViewById(R.id.activity_main_bnTimer);
bnTimer.setOnClickListener(this);
bnAsyncTask = findViewById(R.id.activity_main_bnAsyncTask);
bnAsyncTask.setOnClickListener(this);
@Override
public void onClick(View v)
switch (v.getId())
case R.id.activity_main_bnStartOrStop:
StartOrStop();
break;
case R.id.activity_main_bnRecord:
//计数输出当时获取到的时间
String strRecord = tvRecord.getText().toString();
strRecord += String.format("%02d:%02d.%02d", Minute, Second, CentiSecond) + "\\r\\n";
tvRecord.setText(strRecord);
break;
case R.id.activity_main_bnTimer:
TimerStart();
break;
//定义一个bool值判断当前显示是否在运行状态
private boolean isRunning = false;
//定义一个子线程类的引用变量
private StopwatchThread swThread;
private void StartOrStop()
//启动或者停止
if (!isRunning)
//如果当前线程不是在运行状态,则实例化子线程类对象
swThread = new StopwatchThread();
//调用start启动线程
swThread.start();
//将标志位置为true表示在运行状态
isRunning = true;
tvRecord.setText("");
bnStartOrStop.setText("停止");
else
//如果在运行状态则调用子类对象的Stop方法停止线程
swThread.Stop();
//将标志位置为true表示在停止状态
isRunning = false;
bnStartOrStop.setText("启动");
private void TimerStart()
//定时器启动
if (isRunning)
swThread.Stop();
isRunning = false;
bnStartOrStop.setText("启动");
tvText.setText("3后秒启动");
task = new TimerTask()
@Override
public void run()
Message message = new Message();
message.what = 3;
handler.sendMessage(message);
;
timer.schedule(task, 3000);
运行截图:
以上是关于半期考试 之 多线程的主要内容,如果未能解决你的问题,请参考以下文章