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

Android实现秒表功能

34资源网2022-09-13304

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

分享给朋友:

相关文章

最近北京疫情怎么样了?最新涉及单位聚集性疫情14起

最近北京疫情怎么样了?最新涉及单位聚集性疫情14起

最近我很关注北京的疫情情况,因为那里有我的亲人。相信大家和我一样,都在关注北京疫情每日新增情况,很多人甚至预测北京很可能会出现当初武汉的局面。6月份在疫情严峻时,北京7天新增157病例,那么在这之后疫情是否得到控制了呢?…

让长寿花开花有什么窍门?长寿花开花的几个条件分享

让长寿花开花有什么窍门?长寿花开花的几个条件分享

长寿花虽然说是一种多肉植物,但是它却和其他的很多多肉植物有很多不同的地方,长寿花由肥大、光亮的叶片形成的低矮株丛,终年翠绿。这种植物比较好看,也容易养,是送亲朋好友最好的礼物。马上要12月了,现在市面上卖花卉的很多。很多人不会养长寿花,尤其…

视频号入口在哪里(视频号直播入口新手手册)

视频号入口在哪里(视频号直播入口新手手册)

视频号助手在哪里?视频号助手什么时候上线?微信视频号助手正式上线目前微信视频号助手已经开始内测使用了,大家可以直接在PC端扫码登录,管理自己的视频号,可以看到自己的各项动态数据,非常方便管理。 视频号助手在哪里 视频号助手在哪里? 手机微…

小型电冰箱什么牌子好?现在口碑最好的冰箱推荐

小型电冰箱什么牌子好?现在口碑最好的冰箱推荐

海尔冰箱全球公认的质量很不错的冰箱,海尔冰箱优点是:微霜,制冷效果不错;节能省电,保湿效果不错;海尔冰箱是十大品牌之首,全球销量的第一梯队的佼佼者。海尔冰箱外观十分好看,海尔冰箱款式几乎都很新颖,质量好,制冷情况好,用了一段时间,制冷情况稳…

软件解压后怎么安装(手机解压软件app免费软件)

软件解压后怎么安装(手机解压软件app免费软件)

在PC端宅男、极客们会碰到需要解压缩文件的情况,这个时候大多会用到360压缩或者好压等第三方软件,然而到了移动手机端,由于自带的文件管理器大多不支持输入密码解压缩等复杂操作,让不少人对于手机解压缩文件失望,但是有时候电脑不在身边,又不得不在…

为了让米粉买到好看的手机壳,雷军投了一家“潮玩工厂”

为了让米粉买到好看的手机壳,雷军投了一家“潮玩工厂”

小米团队花了几个月时间,想要找到一个能DIY打印手机壳的设备却没有找到。直到2020年底,他们在西单大悦城看到玩壳工厂。 “用户需求一直存在,早前也有不少团队尝试开发这样的设备,但都失败了。”玩壳工厂创始人CEO韩冰告诉创业邦。 如今,玩壳…