本文介绍了AlarmMenager检查是否已经报警发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个闹钟。
一个主屏幕巫婆列出了所有警报,并添加选项,添加新闹钟上市。
我使用的是报警经理解雇通知或打开的用户界面。
之后用户开放的通知屏幕和命中按钮,返回到主屏幕,并列出所有报警。

I developed an Alarm clock.A main screen witch lists all alarms and add option to add new alarm to list. I am using an Alarm Manager to fire a notification or open user screen.After user open notification screen and hit button, it returns to main screen and list all alarms.

这已经解雇,并且不会再次触发所有的报警,都标有不同的颜色。
要识别巫婆的报警被解雇,我用这code:

All alarms that already fired, and will not fire again, are marked with different color.To identify witch alarms are fired, I am using this code:

How检查是否AlarmMamager已经有一个报警设定?

但是,这code仅后烧制而成报警30秒,返回true(报警设置)立即当我返回到主屏幕返回false(警报未设置)报警。

But this code is returning false (alarm not set) for alarm only after 30 seconds from fired alarm and returns true (alarm set) immediately when i return to main screen.

要设置闹钟我使用这项服务:

To set alarm I am using this service:

protected static void SetBackgroudAlrm(long alarmTime, boolean toggleBtnRep,int AlrmID,Context context) {
    /** Set Alarm in Background */
    AlarmManager manager; 
    PendingIntent pIntent = null ;
        Intent alarmIntent = new Intent(context,AlarmReceiver.class);
        pIntent = PendingIntent.getBroadcast(context, AlrmID, alarmIntent, 0);          

        manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        if (toggleBtnRep){ //repeat is on
            manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,alarmTime ,7 * 24 * 60 * 60 * 1000, pIntent);
        } else { //repeat is off
            manager.set(AlarmManager.RTC_WAKEUP,alarmTime, pIntent);
        }   
        Toast.makeText(MainActivity.getContext(), "Alarm Set ", Toast.LENGTH_SHORT).show();
        //enable automatically resetting alarms when device reboots
       ComponentName receiver = new ComponentName(context, BootReceiver.class);

        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

为了检查闹钟设置,我用这code:

In order to check if alarm is set, I am using this code:

    public static boolean ChkActiveAtrm(int pos){
boolean Rtn = false;
    int  AlrmID[]=ListViewAdapter.GetAlrmSelectID(MainActivity.AlrmIDStr.get(pos),pos);

    for (int i=0;i<AlrmID.length;i++){
        boolean alarmUp = (PendingIntent.getBroadcast(MainActivity.getContext(), AlrmID[i],
                new Intent(MainActivity.getContext(),AlarmReceiver.class),
                PendingIntent.FLAG_NO_CREATE) != null);
        if (alarmUp) {Rtn=true;}
    } //end for

返回RTN;
}

return Rtn;}

有没有人发现这个现象?
谁才能获得报警设定即时显示/不设置?
谢谢

Does anybody else found this phenomenon ?Who do I get immediate indication for alarm set/not set ?Thank you

推荐答案

第一件事,停止依靠 AlarmManager 单独保存和管理报警。有许多场景中的报警将被清除或延迟等。

First thing, stop relying on AlarmManager alone to save and manage alarms. There are many scenarios where alarms will be cleared or delayed etc.

有一个SQLite表等,正确地跟踪报警,一起创建,调度和触发时间戳。

Have a SQLite table etc. to properly track alarms, along with created,scheduled and triggered time stamps.

 ______________________________________________________________________
| id | name | repeating |   time   | interval | set_at  | triggered_at |
 ______________________________________________________________________
| 0  | abc  |     1     |  2394000 |  36000   | 2003900 | 1800094      |
 ______________________________________________________________________

然后:


  1. 实施引导意图接收器读取该表和进度报警每个记录。

  1. Implement a boot intent receiver to read this table and schedule alarms for each record.

当警报设置更新,更新数据库,并重新安排了警钟。

When an alarm setting is updated, update it in database and re-schedule that alarm.

报警被触发时,更新相应的数据库记录的时间戳。

when an alarm is triggered, update time stamps in corresponding database record.

这篇关于AlarmMenager检查是否已经报警发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:14