android测试报警 - 无法激活它

时间:2015-02-26 13:21:01

标签: android timer alert

我在我的应用中设置闹钟以通知用户。我想测试它,但无法弄清楚如何将它设置为当前时间+ 2分钟。我该怎么办?

这是我的代码

Intent myIntent = new Intent(MainActivity.this , PopupWin.class);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
    Calendar calendar = Calendar.getInstance();
  //  calendar.set(Calendar.HOUR_OF_DAY, 13);
   // calendar.set(Calendar.MINUTE, 00);
   // calendar.set(Calendar.SECOND, 00);
    calendar.setTimeInMillis((int)((calendar.getTimeInMillis()+calendar.getTimeZone().getOffset(calendar.getTimeInMillis()))/1000));

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);  //set repeating every 24 hours

2 个答案:

答案 0 :(得分:0)

为什么不尝试创建Timer

首先添加此字段:

private static int delayTime = 2*60*1000 // interval time before the execution of your code is started (in millisecond)

从现在起2分钟后执行代码:

Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        notifyUser(); // notify the user
    }
}, delayTime);

答案 1 :(得分:0)

从当前时间开始两分钟发出警报,

alarmManager.set(AlarmManager.RTC_WAKEUP, (calendar.getTimeInMillis()+(60*2*1000)) , operation);