Android 带有日历的报警管理器的随机时间表
我正试图安排我的AlarmManager每天醒来时的随机时间在22:00-06:00之间 我尝试过这种技术,但似乎不起作用:Android 带有日历的报警管理器的随机时间表,android,alarmmanager,Android,Alarmmanager,我正试图安排我的AlarmManager每天醒来时的随机时间在22:00-06:00之间 我尝试过这种技术,但似乎不起作用: public class AlarmReciver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try { System.out.println(TAG
public class AlarmReciver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
try
{
System.out.println(TAG+": Alert recieved");
schedualeNextTimeAlarming(context);
} catch (Exception e)
{
}
private void schedualeNextTimeAlarming(Context context)
{
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReciver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.cancel(pi);
getNextTime(am, pi);
}
private void getNextTime(AlarmManager am, PendingIntent pi)
{
int max = 8;
int min = 1;
final Random myRandom = new Random();
int result = myRandom.nextInt(max - min) + min;
int hourOfDay = 0;
switch (result)
{
case 0:
hourOfDay = 22;
break;
case 1:
hourOfDay = 23;
break;
case 2:
hourOfDay = 24;
break;
default:
hourOfDay = result;
}
hourOfDay = 15; //for testing
System.out.println("next time alert: "+hourOfDay+" seconds");
Calendar cal = Calendar.getInstance();
// cal.add(Calendar.DAY_OF_YEAR, 1); if i disable this line, the Alarm wont work at all.
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 1);
cal.set(Calendar.SECOND, hourOfDay);
am.cancel(pi);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
}
我的预期结果是,每15秒就会有一个警报。
这是输出:
08-02 11:56:48.188: INFO/System.out(9138): InformationReceiver: Alert recieved
08-02 11:56:48.198: INFO/System.out(9138): next time alert: 15 seconds
08-02 11:56:48.218: INFO/System.out(9138): InformationReceiver: Alert recieved
08-02 11:56:48.228: INFO/System.out(9138): next time alert: 15 seconds
08-02 11:56:48.248: INFO/System.out(9138): InformationReceiver: Alert recieved
08-02 11:56:48.248: INFO/System.out(9138): next time alert: 15 seconds
08-02 11:56:48.278: INFO/System.out(9138): InformationReceiver: Alert recieved
08-02 11:56:48.278: INFO/System.out(9138): next time alert: 15 seconds
08-02 11:56:48.308: INFO/System.out(9138): InformationReceiver: Alert recieved
如你所见,它不会每15秒报警一次
我想在每次进入该功能时,重新安排一个新的报警时间
欢迎提出任何解决办法或其他有效建议
谢谢。已经回答了。要先解除此警报,您需要获取原始实例
public void unsetAlarm(View v) {
Intent myIntent = new Intent(MainActivity.this,
NotificationService.class);
notifyIntent = PendingIntent.getService(MainActivity.this, 0,
myIntent, 0); // recreate it here before calling cancel
alarmManager.cancel(notifyIntent);
Log.v(TAG, "cancelling notification");
}
它已经回答了。要先解除此警报,您需要获取原始实例
public void unsetAlarm(View v) {
Intent myIntent = new Intent(MainActivity.this,
NotificationService.class);
notifyIntent = PendingIntent.getService(MainActivity.this, 0,
myIntent, 0); // recreate it here before calling cancel
alarmManager.cancel(notifyIntent);
Log.v(TAG, "cancelling notification");
}
如果我们知道什么是不起作用的,那将非常有帮助。是否有异常,是否没有警报?有警报,问题是它没有禁用preivouse Alaram。。虽然我真的会癌变。另外,Alaram唤醒是不对的,我将用outpuit编辑代码,这将非常有助于我们了解什么是不起作用的。是否有异常,是否没有警报?有警报,问题是它没有禁用preivouse Alaram。。虽然我真的会癌变。另外,Alaram唤醒不正确,我将使用输出编辑代码