带日历的警报管理器的随机时间表

发布于 2024-09-12 14:04:47 字数 2483 浏览 5 评论 0原文

我试图安排我的 AlarmManager 每天在 22:00 - 06:00 之间发生不同的随机时间唤醒。

我已经尝试过这种技术,但它似乎不起作用:

 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 秒发出一次警报。

我想实现重新安排新的闹钟时间的情况,每次进入此功能时,

任何解决方案或其他有效的建议将非常受欢迎。

谢谢。

I'm trying to schedule my AlarmManager to wake up every day with a diffrent random time which occurs between 22:00 - 06:00.

I've tried this technic, but it doesnt seems to work:

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

}

my expected result is, that every 15 secodns ill have an alarm.
this is the output:

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

As you can see, it doesnt Alarm every 15 seconds.

I wanna achive the situation of re-scheduale a new Alarm time, every time it gets into this function

any solution or other effective suggestions would be very welcome.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单 2024-09-19 14:04:47

它已经回答了这里..。为了首先取消设置此警报,您需要获取原始实例。

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");
    } 

Its already answered Here... In order to unset this alarm first you need to get the Original Instance.

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");
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文