每日重复警报无效

时间:2014-07-14 13:32:39

标签: android alarmmanager repeatingalarm

我正在尝试使用Alarmmanager和BroadcastReciever设置每日重复闹钟,但闹钟没有被正确解雇。

我正在使用以下代码在第一次安装App时设置闹钟:

    checkFirstTime = getSharedPreferences("firstRun", 0);
    db = new DatabaseHandler(this); 
    if (checkFirstTime.getBoolean("my_first_time", true))
    {
        db = new DatabaseHandler(this);
        db.InitializeAll();    
        checkFirstTime.edit().putBoolean("my_first_time", false).commit(); 

        //To set alarm manager
        setAlarmManager();

        Toast.makeText(context, "alarm set", Toast.LENGTH_LONG).show();


    }

功能SetAlarm:

private void setAlarmManager()
    {

          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.set(Calendar.HOUR_OF_DAY, 12);
          calendar.set(Calendar.MINUTE, 00);
          calendar.set(Calendar.SECOND, 0);
          calendar.set(Calendar.AM_PM, 0);

          AlarmManager alarmMgr;

        alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
          Intent intent = new Intent("com.example.testaclrtn");
          PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);



          Calendar now = Calendar.getInstance();
          long calendarTime= 0;

          if(calendar.getTimeInMillis() <= now.getTimeInMillis())
                calendarTime = calendar.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
            else
                calendarTime = calendar.getTimeInMillis() ;

          alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendarTime,
                  AlarmManager.INTERVAL_DAY, pendingIntent);

    }

和BroadcastReciever包含以下代码:

 public class AlarmReceiver extends BroadcastReceiver {

    SharedPreferences storeDay;
    DatabaseHandler db;


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

            Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
            db = new DatabaseHandler(context);
            whenNextDay();

            Message msgToActivity = new Message();
            msgToActivity.what = 5;


     }

    public void whenNextDay()
    {
        int count = db.getPickCount(2);
        int target = db.getPickTarget(2);
        long time = db.getTimeCount(2);
        db.updatePickCount(3, count);
        db.updatePickTarget(3, target);
        db.updateTimeCount(3, time);

        count = db.getPickCount(1);
        target = db.getPickTarget(1);
        time = db.getTimeCount(1);
        db.updatePickCount(2, count);
        db.updatePickTarget(2, target);
        db.updateTimeCount(2, time);

        db.updatePickCount(1, 0);
        db.updateTimeCount(1, 0);
        db.close();


    }

请帮我弄清楚我的逻辑或代码有什么问题。感谢

1 个答案:

答案 0 :(得分:3)

使用此功能设置重复闹钟,每24小时触发一次:

public static void setRepeatingSync(Context context) {
    try {

   AlarmManager alarmMgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), (24 * 1000 * 60 * 60),
                alarmIntent); //every 24 hours
    } catch (Exception e) {
        e.printStackTrace();
    }
}