【问题标题】:how can i get the repeat alarm for week days using alarm manager in android?如何使用 android 中的警报管理器获得工作日的重复警报?
【发布时间】:2012-12-15 15:17:47
【问题描述】:

您好,我是 android 新手,我正在开发警报应用程序,我需要在用户选择的不同日期在同一时间重复调用警报。如果我将闹钟时间设置为上午 10 点并重复选项为周日、周一、周六,我会在每个周日、周一、周六调用闹钟。

这里我使用代码来调用警报

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                Intent intent = new Intent(Alarm.this, OneShotAlarm.class);  //here i am calling broad \cast receiver to invoke alarm

                 PendingIntent sender = PendingIntent.getBroadcast(Alarm.this, requestCode, intent, 0);
                 Calendar calendar = Calendar.getInstance();
calendar.set(Calender.HOUR_OF_DAY,10); 
                am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

通过使用此代码,我在上午 10 点调用当前日期。但我的 gole 需要在上午 10 点调用选定日期的警报。

这里我还有另一个问题,请帮助解决这个问题。当我试图输入过去的时间时,将在当前时间调用警报。我怎样才能给过去的时间。我的意思是当我在 11AP 时,我需要给 9AM。

如果有什么办法,请给我建议。 提前致谢。

【问题讨论】:

  • 您只使用了带有 Sqlite 数据库的 AlarmManager 或 AlarmManager。 sqlite 数据库用于存储事件。
  • 我只使用 AlarmManager 来调用警报。我将时间值存储到 sqlite 以在列表视图中显示警报。但我没有使用 sqlite 中的任何数据来发送待处理的意图
  • 如果你正在使用数据库来存储事件详细信息,那么如果你有兴趣我会给你一个例子。
  • 请给我这个例子,它可能对我的申请有帮助
  • 非常感谢 Dipak Keshariya。你分享的代码对我有用。

标签: android


【解决方案1】:

请在您的广播接收器类中编写以下代码,它将解决您的问题。

public class MyBroadcastReceiver extends BroadcastReceiver {
    DBAdapter mDba;
    SQLiteDatabase mDb;
    Ringtone rt;
    MediaPlayer mp;
    AlertDialog.Builder alertbox;
    Context ctx;

    @Override
    public void onReceive(Context context, Intent intent) {

        DBHelper mDbh = new DBHelper(context, null, null, 1);
        mDb = mDbh.getWritableDatabase();
        mDb.setLockingEnabled(true);
        mDba = new DBAdapter(context);
        mDba.open();
        Cursor cr = mDb.query("mReminderEntry", null, null, null, null,
                null, null);
        if (cr.equals(null)) {
            System.out.println("No Data Found");
        } else {
            Date d = new Date();
            System.out.println("Current Hour is:- " + d.getHours());
            System.out.println("Current Minute is:- " + d.getMinutes());
            Calendar calendar = Calendar.getInstance();
            int day = calendar.get(Calendar.DAY_OF_WEEK);
            String today = null;
            if (day == 2) {
                today = "Monday";
            } else if (day == 3) {
                today = "Tuesday";
            } else if (day == 4) {
                today = "Wednesday";
            } else if (day == 5) {
                today = "Thursday";
            } else if (day == 6) {
                today = "Friday";
            } else if (day == 7) {
                today = "Saturday";
            } else if (day == 1) {
                today = "Sunday";
            }
            System.out.println("Today is:- " + today);

            int system_hour = d.getHours();
            int system_minute = d.getMinutes();
            cr.moveToFirst();
            for (int i = 0; i < cr.getCount(); i++) {
                if (cr.getString(3).equals(system_hour + ":" + system_minute)
                        && cr.getString(1).equals("Daily")) {

                    System.out.println("Matched");
                    Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
                    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(scheduledIntent);

                    break;

                } else if (cr.getString(3).equals(
                        system_hour + ":" + system_minute)
                        && cr.getString(1).equals(today)) {

                    Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
                    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(scheduledIntent);

                    System.out.println("Matched");

                    break;
                } else {
                    System.out.println("No Matching");
                }
                cr.moveToNext();
            }
        }
        cr.close();
        mDba.close();
    }
}

如果你对此有任何问题,请告诉我。

【讨论】:

  • 这个广播是怎么触发的?是每天触发吗?
【解决方案2】:

Dipak Keshariya 是对的,你需要有一些逻辑。一个逻辑可能是:

  • 使用数据库存储警报。

  • 创建一个从数据库获取最新时间的服务。

  • 为此注册警报

  • 触发后,检查警报是否重复

  • 如果是,则更新数据库中的闹钟时间。即现在将为下一个重复日编辑警报。

  • 重启服务

现在如果你的逻辑没问题,那么你不需要回答第二个问题,因为你不需要它。

【讨论】:

    【解决方案3】:

    您可以通过以下方式实现您的重复警报:

    1:首先创建一个数据库并存储您的警报。

    2:然后使用服务,您可以使用时间刻度接收器(广播接收器),在时间刻度接收器的接收方法中,您可以检查当前时间和当前日期,并将其与警报日期和时间相匹配数据库。

    3:如果日期和时间都匹配,则可以触发警报。

    【讨论】:

    • 广播接收器是否每次都检查以比较当前时间和数据库时间。我的申请需要更新时间。广播接收器 onReceiver() 方法是否像每次在后台运行的服务一样每次执行。
    • 时间滴答广播接收器每分钟运行一次,这与一直运行消耗电池寿命的服务不同
    • 避免使用这种方法,因为让应用程序每分钟不断检查是否触发警报是不必要的昂贵解决方案。
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多