Android实现秒表功能
作者:Flying_Master 发布时间:2023-08-21 02:15:41
标签:Android,秒表
本文实例为大家分享了Android实现秒表功能的具体代码,供大家参考,具体内容如下
设计完成一个秒表,具备启停功能,正确使用工作线程完成界面刷新
activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="秒表"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/_00_00_00"
android:textSize="30sp"
android:id="@+id/clock" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清零"
android:id="@+id/init" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计时"
android:id="@+id/start" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:id="@+id/stop" />
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
将activity,service在AndoidMainfest.xml中注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ex_5">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainAActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".TimeService">
</service>
</application>
</manifest>
Timeservice.java
service服务
package com.example.ex_5;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.Date;
public class TimeService extends Service {
@Nullable
private Date startTime = new Date();
private long diff;
public Thread workThread;
private Runnable backGroundWork = new Runnable() {
@Override
public void run() {
while(!Thread.interrupted()){
Date endTime = new Date();
diff = endTime.getTime()-startTime.getTime();
MainActivity.UpdateGUI(diff);
Log.i("TimeService:The diff is",String.valueOf(diff));
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
@Override
public void onCreate() {
super.onCreate();
Log.i("TimeService","onCreate");
workThread=new Thread(null,backGroundWork,"workThread");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if(!workThread.isAlive()){
workThread.start();
}
Log.i("TimeService","onStart");
}
@Override
public void onDestroy() {
super.onDestroy();
MainActivity.UpdateGUI(0);
MainActivity.UpdateDiff(diff);
workThread.interrupt();
Log.i("TimeService","onDestroy");
}
public IBinder onBind(Intent intent) {
return null;
}
}
MainActivity.java
注册按钮响应事件,更新UI界面
package com.example.ex_5;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.net.ServerSocket;
public class MainActivity extends AppCompatActivity {
private static Handler handler = new Handler();
private static TextView labelView = null;
private static String time;
private static long _diff = 0;
//更新界面
public static void UpdateGUI(long diff) {
diff += _diff;
int hours = (int) diff / (1000 * 60 * 60);
int minutes = (int) (diff - (hours * (1000 * 60 * 60))) / (1000 * 60);
int seconds = (int) (diff - (hours * (1000 * 60 * 60)) - (minutes * (1000 * 60))) / 1000;
time = hours + ":" + minutes + ":" + seconds;
handler.post(RefreshLable);
}
//供停止功能使用,用于记录服务结束之时的时间
public static void UpdateDiff(long diff){
_diff = diff;
}
//setText
public static Runnable RefreshLable = new Runnable() {
@Override
public void run() {
labelView.setText(time);
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button initButton = findViewById(R.id.init);
final Button startButton = findViewById(R.id.start);
Button stopButton = findViewById(R.id.stop);
labelView = findViewById(R.id.clock);
final Intent serviceIntent = new Intent(this, TimeService.class);
startButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
Log.i("MainActivity","ClickStartButton");
startService(serviceIntent);
}
});
stopButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Log.i("the behead diff is",String.valueOf(_diff));
Log.i("MainActivity","ClickStopButton");
stopService(serviceIntent);
}
});
initButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Log.i("MainActivity","ClickInitButton");
_diff = 0;
String text = "00:00:00";
labelView.setText(text);
stopService(serviceIntent);
}
});
}
}
来源:https://blog.csdn.net/qq_44660452/article/details/106445533


猜你喜欢
- 闲来无事想玩玩双向通信,实现类似QQ的互发消息的功能。于是乎开始学习.Net Remoting..Net Remoting 是由客户端通过R
- @Entity和@Table注解的用法@Entity注解@Entity注解和@Table注解都是Java Persistence API中定
- 目录1、Integer a = 1;2、对于同一类中的两个方法 , 在判断它们是不是重载方法时 , 肯定不考虑( )3、对于Java中异常的
- 1.介绍我们知道,我们要使一个类型支持foreach循环,就需要这个类型满足下面条件之一:该类型实例如果实现了下列接口中的其中之一:Syst
- Mybatis mapper模糊查询语句LIKE最近做学校安排的课程设计作业,用到SSM框架,在自己写mapper代码是遇到了模糊查询的问题
- 基于 springboot+vue的测试平台开发一、前端环境搭建在前端框架vue-element-admin这个项目中,有一个简洁轻量型的项
- 本文实例为大家分享了Unity3D Ui利用shader添加效果的具体代码,供大家参考,具体内容如下// Upgrade NOTE: rep
- 本文实例为大家分享了Android实现简单旋转动画的具体代码,供大家参考,具体内容如下核心方法public void startAnimat
- Android开发环境有三种方式,分别是JDK+SDK+Eclipse+ADT、JDK+adt-bundle与JDK+Android Stu
- Android的闹钟实现机制, 需要调用AlarmManager.set()将闹铃时间记录到系统中,当闹铃时间到后,系统会给应用程序发送广播
- Example官方介绍Query by Example (QBE) is a user-friendly querying techniqu
- 若 Spring 检测到 bean 实现了 Aware 接口,则会为其注入相应的依赖。所以通过让bean 实现 Aware 接口,则能在 b
- 本文实例为大家分享了android自定义View实现五子棋的具体代码,供大家参考,具体内容如下先说一下吧,android的自定义View就是
- 我们知道HashMap集合是允许存放null值的hashMap是根据key的hashCode来寻找存放位置的,那当key为null时, 怎么
- 最近公司要求开发工具要用Idea,作为一个eclipse的老员工,记录一下Idea中遇到的坑刚开始用Idea从Git上导入一个项目时,遇到了
- 目录通过Resource接口手动加载通过@Value自动转换通过ResourceLoader加载使用ResourceUtils加载资源读取资
- 控制语句——for练习语句的嵌套应用累加求和,计数器循环嵌套一、语句的嵌套应用语句嵌套形式。其实就是语句中还有语句。形式多种多样,没有固定的
- 1. 简单说明嗨,大家好!今天给大家分享的是Mybatis-plus 插件的分页机制,说起分页机制,相信我们程序员都不陌生,今天,我就给大家
- 主要注意的是在资源引用的地方AlertDialog.Builder(this,R.style.dialogNoBg).create();这里
- 一、图示spring再简化:SpringBoot-jar:内嵌tomacat;微服务架构!二、springboot是什么spring是一个为