跟踪多个报警与Android的 [英] Keep track of multiple alarms with Android

查看:100
本文介绍了跟踪多个报警与Android的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置一个数字,重复每周的报警,自动把手机调到震动,当用户在某一类(这是一个学术的应用程序),然后重置卷上课的时候​​结束。

I need to set a number of alarms that repeat weekly, to automatically put the phone on vibrate when the user is on some class (it's an academic app) and then reset the volume when the class finishes.

我已经有被创建的报警,但我必须有一个方式,如果用户禁用此功能停用所有的人。我也知道我必须通过传球和意图与相同参数的AlarmManager.cancel()方法做到这一点,但这里的问题是:

I already have the alarms being created, but I must have a way to deactivate all of them if the user disables this feature. I also know I must do so by passing and intent with the same parameters to the AlarmManager.cancel() method, but the problem here is:

,用户可以取消类和登记在类(但不直接在应用程序),而这更新并反映在DB中,,保持只是当前类。所以,如果我有一个报警设置的一类,不存在了,我怎么可以取消它,如果我不能复制的PendingIntent?

The user can cancel classes and enroll in classes (but not directly in the app), and that's updated and reflected on the DB, that keeps just the current classes. So if I have an alarm setup to a class that doesn't exists anymore, how can I cancel it if I can't replicate the PendingIntent?

一个解决方案我在这里看到的是创建一个数据库表来跟踪当前告警,那么我对他们完全控制,另一种办法是取消并重新设置所有报警,当类列表更新,但这样做所花费的时间相当多,和一个第三,但不那么友好的选择是简单的等待用户启动手机,并在CH375复位报警时,只需设置那些我需要的(正确的,如果我错了,在这个引导行为) 。是否有第四个选项,我应该尝试,或其中之一,如果不够好?

One solution I see here is to create a db table to track the current alarms, then I'd have full control on them, another way would be to cancel and reset all alarms when the class list is updated, but doing so takes a fair amount of time, and a 3rd but less friendly option would be to simple wait for the user to boot the phone, and when reseting the alarms just set the ones I need (correct if I'm wrong on this boot behaviour). Is there a fourth option I should try, or one of these if good enough?

推荐答案

首先,你并不需要设置多个闹钟。只需建立一个报警,然后当火警,成立了新的下报警。这也使得它易于设置报警再次,如果手机重启时你只有一个报警思考时,他们都将丢失。

First you do not need to set up multiple alarms. Simply set up the NEXT alarm and then when that alarm fires, set up the new next alarm. This also makes it easy to set up the alarms again when they are lost if the phone is rebooted as you only have one alarm to think about.

我用这个小程序来把我的报警。在设置参数的值决定是否报警设置或取消。

I use this small routine to set my alarm. The value of the Set parameter determines if the alarm is set or cancelled.

public static void SetMyAlarm(Context c, long AlarmTime, Boolean Set) {
    Intent intent = new Intent(c, AlarmReceiver.class); // The broadcast receiver that will handle my alarm 

    int UniqueID =8192; // id for this specific alarm, use a different id for each separate alarm

    PendingIntent sender = PendingIntent.getBroadcast(c, UniqueID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    if (Set) {
        am.set(AlarmManager.RTC_WAKEUP, AlarmTime, sender);
    } else {
        am.cancel(sender);
    }
}

这篇关于跟踪多个报警与Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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