Android实现秒表功能

本文实例为大家分享了Android实现秒表功能的具体代码,供大家参考,具体内容如下

设计完成一个秒表,具备启停功能,正确使用工作线程完成界面刷新

Android实现秒表功能_第1张图片

activity_main.xml




   

       
   
    

        
    
    

        

AndroidManifest.xml

将activity,service在AndoidMainfest.xml中注册




    
        
            
                

                
            
        
        

        
    

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);
            }
        });
    }


}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android实现秒表功能)