当前位置:首页 > 谈天说地

Android实现秒表功能

34资源网2022-09-13329

本文实例为大家分享了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);
            }
        });
    }


}

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

看完文章,还可以扫描下面的二维码下载快手极速版领4元红包

快手极速版二维码

快手极速版新人见面礼

除了扫码领红包之外,大家还可以在快手极速版做签到,看视频,做任务,参与抽奖,邀请好友赚钱)。

邀请两个好友奖最高196元,如下图所示:

快手极速版邀请好友奖励

扫描二维码推送至手机访问。

版权声明:本文由34楼发布,如需转载请注明出处。

本文链接:https://www.34l.com/post/22059.html

分享给朋友:

相关文章

年轻人不讲武德和耗子尾汁什么意思?什么梗?

年轻人不讲武德和耗子尾汁什么意思?什么梗?

最近经常在朋友圈,抖音上面刷到一些视频评论说年轻人不讲5的和耗子尾汁,小编看着感觉好奇怪,刚开始有点懵逼,结果去网上查了一下才知道,这是太极大师马保国老师讲的。究竟这个梗是怎么来的呢?其实,刚开始人家只是取笑这个马保国大师的梗,后面没有想到…

苹果X快充PD充电器推荐,现在下载还有优惠券可以领呢

苹果X快充PD充电器推荐,现在下载还有优惠券可以领呢

现在有很多人用的是苹果手机,因为平时有些人不注意使用方式或者使用充电器比较频繁导致损坏。大家都知道苹果手机和安卓手机的充电器是不一样的,不能用安卓手机的充电器充苹果手机。所以,大家如果苹果手机充电器损坏了,需要购买的话就要买个专门的苹果手机…

最傻的六种员工离职理由,希望你不是其中一员

最傻的六种员工离职理由,希望你不是其中一员

春节后,今年的中国员工离职率应该是最低的。但是职场上,还是会有很多员工提离职。离职原因各种各样, 不开心,不舒服,工资少,学不到东西等等。那么,最傻的六种员工离职是哪些呢?…

购买须知模板怎么编辑(淘宝买家须知免费素材)

购买须知模板怎么编辑(淘宝买家须知免费素材)

为了帮助您入门,在您注册 Shopify 帐户时,后台的模版页面中会设置一个默认模版。如果您想为在线商店自定义一个不同的模版,则需要向后台添加一个模版。 您可通过以下几种方式添加模版: 如果您的计算机上的 .zip 文件中已有一个模版,那么…