Android實(shí)現(xiàn)秒表功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)秒表功能的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)計(jì)完成一個(gè)秒表,具備啟停功能,正確使用工作線程完成界面刷新

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="計(jì)時(shí)" ? ? ? ? ? ? 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中注冊(cè)
<?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服務(wù)
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
注冊(cè)按鈕響應(yīng)事件,更新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);
? ? }
//供停止功能使用,用于記錄服務(wù)結(jié)束之時(shí)的時(shí)間
? ? 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);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)為L(zhǎng)istView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景
這篇文章主要介紹了Android實(shí)現(xiàn)為L(zhǎng)istView同時(shí)設(shè)置點(diǎn)擊時(shí)的背景和點(diǎn)擊松手之后的背景,以實(shí)例形式較為詳細(xì)的分析了界面元素與功能的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02
AndroidStudio Gradle第三依賴統(tǒng)一管理的實(shí)現(xiàn)方法
這篇文章主要介紹了AndroidStudio Gradle第三依賴統(tǒng)一管理的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-09-09
Android AlertDialog六種創(chuàng)建方式案例詳解
這篇文章主要介紹了Android AlertDialog六種創(chuàng)建方式案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android深入探究自定義View之嵌套滑動(dòng)的實(shí)現(xiàn)
什么是嵌套滑動(dòng)?當(dāng)我們向下滑動(dòng)時(shí),首先是外部的布局向下滑動(dòng),然后才是內(nèi)部的RecyclerView滑動(dòng),向上滑動(dòng)也是如此。這就是嵌套滑動(dòng)的效果2021-11-11
Android Mouse實(shí)現(xiàn)過(guò)程詳細(xì)筆記
鼠標(biāo)的實(shí)現(xiàn)有兩個(gè)步驟,一個(gè)是所有層上面的一個(gè)圖標(biāo),還有一個(gè)就是事件控制2013-09-09
Android ListView常見(jiàn)的優(yōu)化方式詳解
這篇文章主要介紹了Android ListView常見(jiàn)的優(yōu)化方式詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單的觀察者與被觀察者示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單的觀察者與被觀察者,簡(jiǎn)單描述了觀察者模式的概念、原理并結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)觀察者模式的簡(jiǎn)單操作技巧,需要的朋友可以參考下2017-11-11
Android的ListView多選刪除操作實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android的ListView多選刪除操作實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05

