Android实现秒表功能
作者:Flying_Master 时间:2023-08-21 02:15:41
本文实例为大家分享了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
标签:Android,秒表
0
投稿
猜你喜欢
Eclipse git推送上传错误问题解决方案
2022-07-19 15:19:39
Spring自动注入失败的解决方法
2022-08-13 03:41:31
微服务如何通过feign.RequestInterceptor传递参数
2022-02-26 00:28:20
Android实现九格智能拼图算法
2021-11-07 22:40:11
maven中配置项目的jdk版本无效的排查方式
2023-07-18 21:43:42
详解Spring-Boot集成Spring session并存入redis
2021-10-13 07:48:30
通过springboot+mybatis+druid配置动态数据源
2023-06-20 16:48:30
浅谈多线程中的锁的几种用法总结(必看)
2021-12-23 08:05:55
SpringBoot零基础入门之基本操作与概念
2023-10-25 00:41:36
解析Java继承中方法的覆盖和重载
2021-09-02 02:02:32
javaweb Servlet开发总结(一)
2023-04-08 22:52:32
彻底搞懂Java多线程(一)
2023-08-02 10:42:30
手把手带你了解Java-Stream流方法学习及总结
2023-11-25 19:30:15
ActiveMQ安装及部署教程图解
2023-11-17 18:01:46
java开发实现五子棋游戏
2021-09-07 12:41:32
Kotlin类型系统竟如此简单
2021-10-15 20:05:02
浅析Spring Security登录验证流程源码
2023-03-22 01:46:44
使用SpringBoot打jar包并部署到Tomcat详细步骤
2023-12-06 07:15:16
java门禁系统面向对象程序设计
2023-08-25 02:25:53
IDEA离线安装maven helper插件的图文教程
2023-11-28 16:00:24