android 测试警报 - 无法激活它
android test alarm - can't activate it
我在我的应用程序中设置了一个闹钟来通知用户。我想测试它,但不知道如何将它设置为当前时间 + 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
为什么不尝试创建一个 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);
要从当前时间起触发两分钟的警报,请执行此操作,
alarmManager.set(AlarmManager.RTC_WAKEUP, (calendar.getTimeInMillis()+(60*2*1000)) , operation);
我在我的应用程序中设置了一个闹钟来通知用户。我想测试它,但不知道如何将它设置为当前时间 + 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
为什么不尝试创建一个 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);
要从当前时间起触发两分钟的警报,请执行此操作,
alarmManager.set(AlarmManager.RTC_WAKEUP, (calendar.getTimeInMillis()+(60*2*1000)) , operation);