在Android中设置每小时重复的闹钟管理器

xxhby3vn  于 2023-05-15  发布在  Android
关注(0)|答案(4)|浏览(195)

我想在Android中每小时获取一次位置。为此,我使用报警管理器,并设置重复报警每小时,只是想写入文件后,固定时间,即在上午8时和下午12时。我在设置闹钟管理器时遇到了一个问题,虽然我设置为每一个小时,但它在1/2小时内执行。

on button click i start service :

       serviceButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent myIntent = new Intent(AutoMainActivity.this, TrackerService.class);

            pendingIntent = PendingIntent.getService(AutoMainActivity.this, 0, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, ALARM_TRIGGER_AT_TIME,
                    3600000, pendingIntent);

            //3600000 1hrs

            finish();
        }
    });

和服务等级如下:

Tracker Service.class

String FINAL_STRING;
SharedPreferences pref;
static final int START_TIME = 8;
static final int MID_TIME = 12;

    java.util.Date systemDates = Calendar.getInstance().getTime(); 
    int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

    if(hour == START_TIME)
        {
            edit.putString("smsdata", FINAL_STRING);
            edit.commit();

            //sendSms(START_TAG+pref.getString("smsdata", ""));
            edit.putString("smsdata", "");
            edit.commit();
        }else {

            System.out.println("currentdate:"+simpleDateFormat.toString());
            System.out.println("current_time:"+currentTime);

            Editor edit = pref.edit();
            edit.putString("smsdata", pref.getString("smsdata", "")+FINAL_STRING+"#");
            edit.commit();

            if(hour==MID_TIME)
            {
                //sendSms(START_TAG+pref.getString("smsdata", ""));
                generateNoteOnSD("\n"+START_TAG+pref.getString("smsdata", ""));
                edit.putString("smsdata", "");
                edit.commit();
                System.out.println("mid time");

            }

        }

当我执行此操作时,服务每30分钟启动一次。但我想每60分钟上一次。

6ss1mwsb

6ss1mwsb1#

首先,您确实希望使用一个可用的常量,如INTERVAL_HOURsetInexactRepeating()
其次,setInexactRepeating()是不精确的。Android保留调整闹钟时间的权利,以便将事件与其他计划的不精确闹钟合并。
因此,尝试短暂切换到setRepeating()。如果它现在像您期望的那样工作,那么您的行为是由于setInexactRepeating()的“不精确”性质造成的。
也可以使用**adb shell dumpsys alarm**查看定时告警。你可能有两个闹钟,每个闹钟每小时响一次。

ego6inou

ego6inou2#

setInexactRepeating()是它不按预期工作的原因。尝试以下操作:

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, firstStart, interval, pendingIntent );
ppcbkaq5

ppcbkaq53#

在我的应用程序中,我有在4种情况下触发警报的功能:
1.对于用户选择的日期和时间,仅执行一次
1.每天选择时间
1.根据所选日期和时间每周一次
1.用户选择的自定义星期几我使用以下方法成功实现了前3个方案:

仅一次:

Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.YEAR, Integer.parseInt(date[0]));
        calendar.set(Calendar.MONTH, (Integer.parseInt(date[1])) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[2]));
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
        calendar.set(Calendar.SECOND, 0);

        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

日调度:

Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
            calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
            calendar.set(Calendar.SECOND, 0);

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

周计划(按系统日期):

Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
        calendar.set(Calendar.SECOND, 0);
        //long interval = calendar.getTimeInMillis() + 604800000L;
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);

如何为一周中的自定义日期实施每周闹钟计划?

private void scheduleAlarm(int dayOfWeek) {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);

    // Check we aren't setting it in the past which would trigger it to fire instantly
    if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
        calendar.add(Calendar.DAY_OF_YEAR, 7);
    }

    // Set this to whatever you were planning to do at the given time
    PendingIntent yourIntent; 

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, yourIntent);
}

private void setUpAlarms() {

    scheduleAlarm(Calendar.MONDAY);
    scheduleAlarm(Calendar.FRIDAY);
}

署名来源:https://microeducate.tech/repeating-alarm-for-specific-days-of-week-android/,问题作者:Dhruvil Patel(https://stackoverflow.com/users/1367157/dhruvil-patel),答案作者:MBH(https://stackoverflow.com/users/2296787/mbh

rfbsl7qr

rfbsl7qr4#

要在Android中设置每小时的重复闹钟管理器,您可以使用以下代码:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, YourBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

// Set the alarm to start at the top of the hour
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.HOUR_OF_DAY, 1);

// Set the alarm to repeat every hour
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR, pendingIntent);

在这段代码中,我们首先获取AlarmManager系统服务的一个示例。然后,我们创建一个Intent,它将在触发警报时广播。我们从这个Intent创建一个PendingIntent,它将用于设置闹钟。
然后我们创建一个Calendar对象,并将其设置为整点。我们在此时间上增加一个小时,以确保警报每小时触发一次。然后,我们使用AlarmManagersetRepeating()方法将闹钟设置为每小时重复一次。
最后,我们将PendingIntent传递给setRepeating()方法沿着传递的还有警报首次触发的时间和重复的间隔。

相关问题