網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2023-03-27 Android進階之從IO到NIO的模型機制演進_Android
- 2022-03-09 C#判斷DLL文件是32位還是64位的示例代碼_C#教程
- 2023-03-26 python3.7環境下sanic-ext未生效踩坑解析_python
- 2023-07-24 uniapp開發小程序,包過大解決方案
- 2022-05-23 python列表排序用?sort()和sorted()的區別_python
- 2022-07-12 CSS樣式:彈性容器上的樣式
- 2023-01-02 詳解LeakCanary分析內存泄露如何實現_Android
- 2023-01-30 python?集合常用操作匯總_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支