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
0
投稿
猜你喜欢
- Mybatis所需要的jar包:需要引用两个jar包,一个是mybatis,另一个是MySQL-connector-Java,如果是mave
- 方法一:使用AnimatedGif库Nuget安装包:Install-Package AnimatedGif -Version 1.0.5h
- 1. List1.1 List 的常见方法方法描述boolean add(E e)尾插 evoid add(int index, E ele
- SpringBoot找不到javax.servlet.Filter的问题新创建一个SpringBoot项目,编译时出现了找不到javax.s
- 引言最近在写一个 Mybatis 代码自动生成插件,用的是Mybatis来扩展,其中有一个需求就是 生成javaMapper文件和 xmlM
- Web UI项目中, 很多 Spring controller 视图函数直接返回 html 页面, 还有一些视图函数是要重定向或转发到其他的
- 对于单链表不熟悉的可以看一下基于Java实现单链表的增删改查一、原地反转1、新建一个哨兵节点下一结点指向头结点2、把待反转链表的下一节点插入
- 一、同步调用1、同步调用会按照代码顺序来执行2、同步调用会阻塞线程,如果是要调用一项繁重的工作(如大量IO操作),可能会让程序停顿很长时间,
- Lucene从今天开始,我们要开始介绍Lucene中索引构建的流程。因为索引构建的逻辑涉及到的东西非常多,如果从构建入口IndexWrite
- Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日
- 下面通过图文并茂的方式给大家讲解下Java开发环境配置,具体内容如下:对于JAVA新手来说,刚开始要学JAVA,而自己的电脑上毫无与JAVA
- 这篇文章主要介绍了JAVA使用 * 对象进行敏感字过滤代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 前言经过一段时间的学习Oauth2,在网上也借鉴学习了一些大牛的经验,推荐在学习的过程中多看几遍阮一峰的《理解OAuth 2.0》,经过对O
- 一、项目搭建1、新建模块2、导入依赖 :将不相关的依赖删掉<!-- <de
- 在项目开发中,我们经常会遇到表中的字段名和表对应实体类的属性名称不一定都是完全相同的情况,下面小编给大家演示一下这种情况下的如何解决字段名与
- 简介本文介绍微服务架构中如何实现单点登录功能创建三个服务:操作redis集群的服务,用于多个服务之间共享数据统一认证中心服务,用于整个系统的
- 1,在项目上鼠标右键 --> Open Module Settings2, Artifacts --> + --> JAR
- 目录MVC结构简介SpringMVC简介SpringMVC执行流程常用注解1. @Controller2. @ResponseBody3.
- Comparable 简介Comparable 是排序接口。若一个类实现了Comparable接口,就意味着“该类支持排序”。
- 网上找了一些获取客户端IP的方法,但本地测试时,返回的是IPv6的表示方法"::1":Host文件里面:# &