如何打印报警管理器信息?

时间:2015-01-20 08:20:58

标签: android alarmmanager

我使用Alarm Manager创建了多个警报。但在通知之前,我不知道它是否成功创建。是否可以在申请中打印报警管理器的信息(即日期,时间,状态)?

3 个答案:

答案 0 :(得分:2)

使用adb shell dumpsys alarm

这将为您提供AlarmManager

中所有活动闹钟的列表

答案 1 :(得分:1)

检查这些问题,

Get list of active PendingIntents in AlarmManager

How to check if Alarm have been set and running

或者我们可以这样做

Intent intent = new Intent("com.my.package.MY_UNIQUE_ACTION");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, 
                                  intent,     PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 1);

AlarmManager alarmManager = (AlarmManager)  context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,    calendar.getTimeInMillis(), 1000 * 60, pendingIntent);

用于检查上述警报是否有效我们可以这样做

boolean alarmUp = (PendingIntent.getBroadcast(context, 0, 
    new Intent("com.my.package.MY_UNIQUE_ACTION"), 
    PendingIntent.FLAG_NO_CREATE) != null);

if (alarmUp) {
Log.d("myTag", "Alarm is already active");
}

表示如果描述的PendingIntent尚不存在的标志,则只返回null而不是创建它

答案 2 :(得分:0)

我认为这是你的解决方案

Calendar calendar = Calendar.getInstance();
Long preTime = calendar.getTimeInMillis();

// set alarm after 5 minute
calendar.add(Calendar.MINUTE, 5);
    Long postTime = calendar.getTimeInMillis();
    Long delay = postTime - preTime;
AlarmManager manager = (AlarmManager)     getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.RTC_WAKEUP, postTime, null);
CountDownTimer timer = new CountDownTimer(delay, 1) {
@Override
        public void onTick(long millisUntilFinished) {
            final int seconds = (int) (millisUntilFinished / 1000) % 60;
            final int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
runOnUiThread(new Runnable() {
@Override
                public void run() {
                    text.setText("minute " + minutes + " Second " + seconds);
                }
            });
        }
@Override
        public void onFinish() {
            // TODO Auto-generated method stub
}
    };
    timer.start();

http://www.wenda.io/questions/4377820/show-left-time-until-the-alarm-start.html