我如何知道直到闹钟响起还剩多少秒?

时间:2018-06-19 12:51:39

标签: java android alarmmanager

场景

我正在开发一款类似于订阅的应用程序,我已经启动了几天的警报管理器,假设警报管理器昨天已启动,明天应该关闭,我怎么知道直到警报管理器关闭的确切时间呢?

@font-face {
    font-family: 'Open Sans';
    font-style: normal;
    font-weight: 600;
    src: url('../fonts/open-sans-v15-latin-600.eot?v=1.101'); /* IE9 Compat Modes */
    src: local('Open Sans SemiBold'), local('OpenSans-SemiBold'),
    url('../fonts/open-sans-v15-latin-600.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
    url('../fonts/open-sans-v15-latin-600.woff2?v=1.101') format('woff2'), /* Super Modern Browsers */
    url('../fonts/open-sans-v15-latin-600.woff?v=1.101') format('woff'), /* Modern Browsers */
    url('../fonts/open-sans-v15-latin-600.ttf') format('truetype'), /* Safari, Android, iOS */
    url('../fonts/open-sans-v15-latin-600.svg#OpenSans') format('svg'); /* Legacy iOS */
}

我使用这种方法检查警报管理器是否在0后台运行。

// ALARM_MANAGER setting to expired
SharedPreferences alarmpreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor alarmEditor = alarmpreferences.edit();
alarmEditor.putString("ALARM_MANAGER", "active");
alarmEditor.apply();

AlarmManager service = (AlarmManager) getApplicationContext()
                            .getSystemService(getApplicationContext().ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(), MyReceiver.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), mainAlarmRequestCode, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 20 seconds after boot completed
int secondTime = Integer.parseInt(expireTime)*60;
cal.add(Calendar.SECOND, secondTime);
//
// Fetch every 20 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pending);

警报管理器处于活动状态,但是我怎么知道直到警报响起还剩秒数?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以尝试存储AlarmManager的失效日期并定期进行测试,以检查当前日期是否大于失效日期。