代号One-应用程序在背景中或智能手机空闲时的UITimer [英] Codename One - UITimer when the app is in backgroud or the smartphone is idle

查看:57
本文介绍了代号One-应用程序在背景中或智能手机空闲时的UITimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与上一个问题有关:

This question is related to a previous one: Codename One - Notify something to the user every five minutes

我发现 UITimer 不会

请考虑以下代码(在Android上测试):

Consider the following code (tested on Android):

public void start() {
        if (current != null) {
            current.show();
            return;
        }
        Form hi = new Form("Five minutes alert", BoxLayout.y());
        hi.add(new Label("Alert every five minutes..."));
        Button button = new Button("Go to background");
        button.addActionListener(l -> {Display.getInstance().minimizeApplication();});
        hi.add(button);
        hi.show();
        sound();
        UITimer.timer(1000 * 60 * 5, true, () -> {
            sound();
        });
        Display.getInstance().minimizeApplication();
    }

    private void sound() {
        try {
            Media m = MediaManager.createMedia((Display.getInstance().getResourceAsStream(getClass(), "/bell.mp3")), "audio/mpeg");
            m.play();
        } catch (IOException err) {
            Log.e(err);
        }
    }

问题是,当打开应用程序时,<播放code> bell.mp3 之后,应用程序自动进入后台,几秒钟后(根据Android设置),智能手机进入空闲状态(即黑屏),并且五分钟后未调用 sound()方法。当我唤醒智能手机(按下电源按钮并在屏幕上移动一根手指)并且仅在按下主页按钮并将应用程序置于前台后,才会调用该功能。 如果应用程序已经在前台但智能手机处于空闲状态(这是用户不使用它时的正常状态),问题也是一样的。

The problem is that when the app is opened the bell.mp3 is played, after that the app goes automatically to background, the smartphone becames idle (that means black screen) after few seconds (according to the Android settings), and the sound() method is not called after five minutes. It will be called when I wake up the smartphone (pressing the power button and moving one finger on the screen) and only after pressing the "home" button and putting the app on foreground. The problem is the same if the app is already on foreground but the smartphone is idle (that is its normal status when the user is not using it).

所以,这是我的问题:我需要执行 sound()才能每五分钟自动播放一次声音(或执行其他操作)。正确的方法是什么?

So, this is my question: I need to execute the sound() to play automatically a sound every five minutes (or to do something else). What is a correct method to do it?

推荐答案

EDT不在后台运行,因此没有 UITimer callSerially 等。如果我们运行了EDT,我们会消耗电池资源,并且操作系统会杀死该应用程序,因此您不需要这样做。

The EDT doesn't run in the background so no UITimer no callSerially etc. If we did run the EDT we'd consume battery resources and the OS would kill the app so that isn't something you'd want.

您可能正在寻找的是背景音乐,它在此处的背景音乐部分

What you are probably looking for is background music which is discussed in the background music section here.

这篇关于代号One-应用程序在背景中或智能手机空闲时的UITimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆