【问题标题】:How do I know the number of seconds remaining until the alarm goes off?我如何知道距离闹钟响起的剩余秒数?
【发布时间】:2018-06-20 00:46:14
【问题描述】:

场景

我正在开发一个类似于订阅的应用程序,我启动了几天的警报管理器,假设警报管理器昨天启动并且明天应该会关闭,我如何知道警报管理器关闭之前的确切时间?

// 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);

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

Intent i = new Intent(getApplicationContext(), MyReceiver.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(), mainAlarmRequestCode, i, PendingIntent.FLAG_NO_CREATE) != null);


if (alarmUp) {
   Log.d("myTag", "Alarm is already active");
   Toast.makeText(this, "alarm manager is active!", Toast.LENGTH_SHORT).show();

//alarm is active check alarm manager counter time


} else {
   Toast.makeText(this, "alarm manager is not active!", Toast.LENGTH_SHORT).show(); }

警报管理器处于活动状态,但我如何知道距离警报响起的剩余秒数?

任何帮助将不胜感激!

【问题讨论】:

标签: java android alarmmanager


【解决方案1】:

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

【讨论】:

  • 感谢您的回复,但我不想运行任何后台服务,因为它会消耗电池!我想知道是否有一种方法可以让我们获得警报管理器的剩余时间。
  • 根据文档,没有这样的方法可以实现您想要实现的目标。但是,如果您只想拥有一次剩余时间,则可以阅读此答案:stackoverflow.com/a/24493985/5455101
  • 好的,谢谢,我正在研究以找出解决方案或解决方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多