显示“离开时间”直到闹钟开始

时间:2014-03-06 12:00:50

标签: android alarmmanager android-alarms

我想在Android中创建一个闹钟(AlarmManager)。 警报必须显示完成前的剩余时间。

因此,用户点击“在晚上11点设置闹钟”并在TextView中查看“以xy小时/分钟/秒为单位的闹钟”。

我怎样才能做到这一点?

感谢。

2 个答案:

答案 0 :(得分:3)

试试这个

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

答案 1 :(得分:2)

//INIT
Calendar c = Calendar.getInstance();
int hNow = c.get(Calendar.HOUR_OF_DAY);    //get the Hour
int mNow = c.get(Calendar.MINUTE);         //get the Minute

int hAlarm = 8;       //your alarm Hour
int mAlarm = 30;      //your alarm Minute 

//FUNCTION

int timeLeft = (hAlarm * 60 + mAlarm) - (hNow * 60 + mNow);
if( timeLeft < 0 )
   timeLeft = 1440 + timeLeft;
int hLeft = timeLeft / 60;      //this is the hours left
int mLeft = timeLeft % 60;      //this is the minutes left