Android:使用线程在指定时间做某事

标签 android multithreading alarm

我的应用程序中有一个数据库,我在其中存储提醒之类的内容。其中一列是应该“提醒”提醒的时间的字符串表示形式,如下所示:hh:mm。我在我的主要 Activity 中创建了一个线程,以定期监视所有提醒并检查是否到了设置警报的时间。在我创建这个线程之前,我将时间 + 所有数据库行的 ID 加载到一个 ArrayList 中,我在我的线程中使用这个 ArrayList 而不是数据库本身(我在这样做时遇到了一些问题)。无论如何,这是代码:

首先,我使用 Application 类声明全局变量:

public class MyApplication extends Application {
    public ArrayList<String> reminders = new ArrayList<String>();
    public int hour;
    public int minute;
}

在我的主要 Activity 中:

public class Home extends Activity {

    ArrayList<String> reminders;
    String time:
    int hour;
    int minute;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //The usual code at the beginning of onCreate method

        //I load my global variables
        MyApplication appState = ((MyApplication)getApplicationContext());
        reminders = appState.reminders;
        hour = appState.hour;
        minute = appState.minute;

        //I save curren time into global variables
        Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR);
        minute = c.get(Calendar.MINUTE);

        //I loop over all rows of database and save what I need from them into
        //Strings in ArrayList reminders. I do this only once on Application
        //launch to load already existing rows. When the application runs
        //I can always add or remove existing rows using special Activity

        //I create and start my Thread
        Thread t = new Thread() {
            try {
                while (true) {
                    time = hour + ":" + minute;
                    if (reminders.size() > 0) {
                        for (int i = 0; i < reminders.size(); i++) {
                            if (reminders.get(i).contains(time)) {
                                //One of the Strings in ArrayList reminders
                                //contains String representation of current
                                //time (along with the ID of database row).
                                //Here I will probably be starting a new
                                //Activity
                            }
                        }
                    }
                    minute++;
                    if (minute == 60) {
                        minute = 0;
                        hour++;
                    }
                    if (hour == 24) {
                        hour = 0;
                    }
                    sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        t.start();
    }
}

它似乎工作正常,但我真的不喜欢这个解决方案。我的第一个问题是是否有任何方法可以改进这段代码?我可以在 Thread 本身中创建变量 int hour、int minute 和 ArrayList reminders,然后在 Thread 循环序列之前加载提醒内容吗?这样我就不必使用 Application 类来存储变量,但我需要它们是全局的,即使当我在我的应用程序中启动新的 Activity 时线程也需要运行并且它需要正确存储这些变量。

我的第二个问题是,您是否会推荐一些完全不同的方法来解决这个问题。

非常感谢!


我想在我的问题中添加一些内容。由于我将使用 AlarmManager,因此我需要在不止一个 Activity 中设置重复事件。所以我的问题是,我应该在每个 Activity 中使用不同的 AlarmManager 实例来添加或删除事件,还是应该使用全局声明的相同实例?谢谢。

最佳答案

AlarmManager 是定期执行任务的更好选择,例如检查表中的“提醒”并在必要时提醒用户。原因是当 CPU 进入休眠状态时 Thread 不会运行。如果你想让你的线程保持清醒,你需要一个 WakeLock,这会非常耗电。 AlarmManager 对此进行了优化。

其次,您不需要全局变量。所以请不要扩展应用程序。这不是必需的。

关于Android:使用线程在指定时间做某事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9953832/

相关文章:

android - 使用 Fitness API 的 Google 登录请求总是失败

android - Spotify 登录错误 INVALID_CLIENT : Invalid redirect URI android

iphone - 为 iPhone 创建闹钟应用程序

python - 如何在 Python 中优化多处理

java - 为警报应用程序提供默认音调

黑莓 - 如何创建闹钟事件?

android - Android 中 RelativeLayout 问题中的 EditText

Android 视频采集示例应用

java - Thread.join() 之后实际发生了什么?

java - Spring 注入(inject)的 bean 线程安全