You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发如何获取当前时间并判断是否等于指定时间触发音频播放

实现代码修正方案

首先你原有代码存在3个核心问题:

  • 创建了SimpleDateFormat格式化对象但未实际使用,调用系统getTimeInstance()返回的时间格式不固定,无法和固定格式的目标时间匹配
  • Java中字符串内容比较需要用equals()方法,==比较的是字符串的内存地址,会出现判断失效
  • 时间判断逻辑写在onCreate中仅会在页面启动时执行一次,且点击监听仅在启动时符合条件才设置,页面运行过程中时间走到目标值时逻辑不会生效

修正后的完整代码

package com.example.simplecalculatorbyvalentinlearningapp;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class SeccondScreen extends AppCompatActivity {

    TextView h;
    TextView d;
    // 全局时间格式化对象,避免重复创建
    SimpleDateFormat hourFormat = new SimpleDateFormat("HH:mm", Locale.CHINA); // HH为24小时制,换成hh就是12小时制
    SimpleDateFormat fullDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a", Locale.CHINA);
    private android.os.Handler timeHandler;
    private Runnable timeUpdateTask;
    private MediaPlayer betaaudio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seccond_screen);

        // 初始化音频
        betaaudio = MediaPlayer.create (this, R.raw.betaaudio);

        h = findViewById(R.id.hour);
        d = findViewById(R.id.date);

        // 初始化显示时间
        updateTimeDisplay();

        // 给时间文本设置点击监听
        h.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 每次点击都重新获取当前时间,保证判断准确
                String currentHour = hourFormat.format(Calendar.getInstance().getTime());
                // 判断是否等于目标时间,比如14:15
                if (currentHour.equals("14:15")) {
                    betaaudio.start();
                }
            }
        });

        // 可选:添加定时任务实现时间实时刷新,1分钟更新一次
        timeHandler = new android.os.Handler();
        timeUpdateTask = new Runnable() {
            @Override
            public void run() {
                updateTimeDisplay();
                timeHandler.postDelayed(this, 60 * 1000);
            }
        };
        timeHandler.post(timeUpdateTask);
    }

    /**
     * 更新页面上的时间显示
     */
    private void updateTimeDisplay() {
        Calendar now = Calendar.getInstance();
        // 设置小时文本
        h.setText(hourFormat.format(now.getTime()));
        // 设置日期文本
        d.setText(fullDateFormat.format(now.getTime()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 移除定时任务避免内存泄漏
        if (timeHandler != null && timeUpdateTask != null) {
            timeHandler.removeCallbacks(timeUpdateTask);
        }
        // 释放音频资源
        if (betaaudio != null) {
            betaaudio.release();
            betaaudio = null;
        }
    }
}

内容的提问来源于stack exchange,提问作者Valy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 21:54:04