首页 > 其他分享 >直播平台软件开发,Android 计时器,定时功能

直播平台软件开发,Android 计时器,定时功能

时间:2023-02-16 14:11:25浏览次数:32  
标签:layout 软件开发 timer public chjTimerInter 计时器 time Android android

直播平台软件开发,Android 计时器,定时功能

 计时器工具类

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
 
import androidx.annotation.NonNull;
 
import java.util.Timer;
import java.util.TimerTask;
 
/**
 * 用于计时,在主线程中使用此方法
 */
public class ChjTimer {
 
    private int time;//设置倒计时 X 秒
    private int interval = 1000;//设置间隔时间
    private ChjTimerInter chjTimerInter; //回调
    private Timer timer; // 定时器
    private static final int WHAT_REFREH = 0;//刷新
 
    /**
     * 创建对象则开始计时
     *
     * @param chjTimerInter 接口回调
     */
    public ChjTimer(ChjTimerInter chjTimerInter) {
        this.chjTimerInter = chjTimerInter;
    }
 
    /**
     * 创建对象开始计时
     * @param interval      间隔时间通知(使用第一个方法,默认1秒钟刷新一次)
     * @param chjTimerInter 接口回调
     */
    public ChjTimer(int interval, ChjTimerInter chjTimerInter) {
        this.chjTimerInter = chjTimerInter;
        this.interval = interval;
    }
 
    /**
     * 开始计时
     */
    public void start(int time) {
        this.time = time;
        if (timer == null){
            timer = new Timer();
        } else {
            stop();
            return;
        }
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                timesss.sendMessage(new Message());
            }
        }, interval);
    }
 
    /**
     * 终止计时
     */
    public void stop() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timesss != null) timesss.removeMessages(WHAT_REFREH);
        if (chjTimerInter != null)chjTimerInter.stop(time);
    }
 
    @SuppressLint("HandlerLeak")
    private Handler timesss = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what != WHAT_REFREH) return;
            time -= 1;
            if (chjTimerInter != null) chjTimerInter.second(time);
            if (time == 0) {
                if (timer == null) return;
                timer.cancel();
                timer = null;
                if (chjTimerInter != null) chjTimerInter.expire();
            } else if (time > 0) {
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        timesss.sendMessage(new Message());
                    }
                }, interval);
            }
        }
    };
 
    /**
     * 接口
     */
    public interface ChjTimerInter {
 
        /**
         * 间隔时间内回调
         */
        void second(int time);
 
        /**
         * 完成回调
         */
        void expire();
 
        /**
         * 终止计时
         */
        void stop(int time);
 
    }
 
}

​页面 布局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/timnew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计时完成"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <TextView
        android:id="@+id/time"
        android:text="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
 
    <Button
        android:id="@+id/but"
        android:text="开始"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <Button
        android:id="@+id/buts"
        android:text="终止"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</LinearLayout>

以上就是 直播平台软件开发,Android 计时器,定时功能,更多内容欢迎关注之后的文章

 

标签:layout,软件开发,timer,public,chjTimerInter,计时器,time,Android,android
From: https://www.cnblogs.com/yunbaomengnan/p/17126470.html

相关文章

  • android 开发问题
    viewpager滑动时中间的一闪而过开发时遇到了这个问题,百思不得其解,后来查看源码/***Setthecurrentlyselectedpage.**@paramitemItemindex......
  • 【android】关于Android中将PCM转AMR的问题探讨
    因为项目中用到了阿里的语音识别技术,但是返回的是PCM原始数据,这里就需要将PCM转为AMR的音频格式,这里将其中遇到的问题记录下来,有需要的小伙伴们可以参考一下。这里的实现......
  • 【android 】Android PCM文件转WAV文件
    之前对android音频方面根本不了解,找了很多博客都没找到合适自己的,顺便看了很多代码,发现pcm基本上所有的音频格式都能转,我项目里面用到的就是wav的,所以这里贴一下pcm转wav......
  • Android  JetPack~ LiveData (一)   介绍与使用
    一般情况下LiveData都是搭配这ViewModel使用,这里先介绍一下LiveData,再结合ViewModel使用Android数据绑定技术一,企业级开发Android数据绑定技术二,企业级开发Androi......
  • Android material design转场动画
    前言Android入门的简单activity跳转太过单调,因此学习使用Google专门设计的转场动画简单使用在触发动画的组件xml上声明转场动画标识<Button android:id="@+id/login_r......
  • Android面试-字节一面
    距离上次跳槽已经过了3年多,突然看到字节的HR来捞。想着自己好久没面了,就打算去试试看。0.准备视频面试,不用去现场真的太赞了。由于项目比较忙,自己又不是特别想跳槽,所......
  • Android 编译系统分析
    在编译androidproject时,我们需要使用到makefile文件,通过makefile文件的规则来构建整个projectsourcebuild/envsetup.shlunchproject_namemake-jxenvsetup.sh脚本......
  • Android 监听短信数据库过滤获取短信内容上传至服务器
    前言Android监听短信的方式有两种1、监听短信数据库,数据库发生改变时回调。2、监听短信广播其中第二种方式由于国内各厂家的定制Android可能导致无响应目前测试魅......
  • 【android】Android pcm转wav格式方法
    程序中的录音文件之前直接保存的是.pcm格式,一直也相安无事,用的挺好。最近有一个webview中需要加载录音文件,出现了不兼容的问题,所以需要把.pcm格式文件转换为.wav格式。......
  • 2-STM32+Air724UG基本控制篇(自建物联网平台)-整体运行测试-Android扫码绑定Air724,并
    <p><iframename="ifd"src="https://mnifdv.cn/resource/cnblogs/ZLAir724UGA/my.html"frameborder="0"scrolling="auto"width="100%"height="1500"></iframe></p>......