如何使用实际本地时间作为报警管理器而不是设备设置时间

时间:2022-03-03 16:31:23

I am using an alarm manager to start a service at particular time which is working fine, but for now if user changes the device time the service gets started according to that time.

我正在使用警报管理器在特定时间启动服务,该服务工作正常,但是现在如果用户更改设备时间,则服务将根据该时间启动。

Code:

calendar = new GregorianCalendar();
    calendar.setTimeInMillis(System.currentTimeMillis());
    cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY, 11);
    cal.set(Calendar.MINUTE, 57);
    cal.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND));
    cal.set(Calendar.DATE, calendar.get(Calendar.DATE));
    cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    Intent it = new Intent(MainActivity.this,Start_service_alarm.class);

    it.putExtra(Start_service_alarm.ACTION, Start_service_alarm.ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, it, 0);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 

1 个解决方案

#1


The device only knows the currently set time (which can be automatically determined from an external internet or mobile network source, but can also be overridden by the user).

设备仅知道当前设置的时间(可以从外部互联网或移动网络源自动确定,但也可以由用户覆盖)。

You need to have an external time provider that your application syncs with, probably sending its location so that the server can determine the correct time zone.

您需要具有应用程序同步的外部时间提供程序,可能需要发送其位置,以便服务器可以确定正确的时区。

There are apis already available, e.g.: http://timezonedb.com/api, or you could roll your own

有apis已经可以使用,例如:http://timezonedb.com/api,或者你可以自己动手

#1


The device only knows the currently set time (which can be automatically determined from an external internet or mobile network source, but can also be overridden by the user).

设备仅知道当前设置的时间(可以从外部互联网或移动网络源自动确定,但也可以由用户覆盖)。

You need to have an external time provider that your application syncs with, probably sending its location so that the server can determine the correct time zone.

您需要具有应用程序同步的外部时间提供程序,可能需要发送其位置,以便服务器可以确定正确的时区。

There are apis already available, e.g.: http://timezonedb.com/api, or you could roll your own

有apis已经可以使用,例如:http://timezonedb.com/api,或者你可以自己动手