【问题标题】:how to do something at specific time for everyday in android如何在android中每天的特定时间做某事
【发布时间】:2014-09-25 21:10:25
【问题描述】:

我想在每天上午 9:00 检查一些网址。但是我该怎么做呢?我确实检查了一些关于 AlarmManager 和 IntentService 的主题和文章,但我无法为我的应用程序聚在一起,是否有任何示例代码或其他内容?谢谢..

【问题讨论】:

  • AlarmManager 有什么问题?
  • AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, Zamanlayici.class); intent.putExtra(ONE_TIME, Boolean.FALSE); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); //5秒后 am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pi);就像这段代码,我可以在这个特定的时间之间做,但我不能在这个特定的时间里做
  • 因此,创建一个Calendar 对象,作为您第一个事件的特定时间,并使用INTERVAL_DAY 的时间段使其每24 小时重复一次。
  • 你的日历对象是什么?

标签: java android


【解决方案1】:

试试这个:

 /Create alarm manager

 AlarmManager mAlarmManger = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

 //Create pending intent & register it to your alarm notifier class
 Intent intent = new Intent(this, AlarmReceiver_maandag_1e.class);
 intent.putExtra("uur", "1e"); // if you want
 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

 //set timer you want alarm to work (here I have set it to 9.00)
 Calendar calendar = Calendar.getInstance();
 calendar.set(Calendar.HOUR_OF_DAY, 9);
 calendar.set(Calendar.MINUTE, 0);
 calendar.set(Calendar.SECOND, 0);

 //set that timer as a RTC Wakeup to alarm manager object
 mAlarmManger .set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

【讨论】:

  • 警告:这种方法没有考虑到calendar可能已经过去了。一天中的大部分时间都是这种情况。您需要检查calendar 是否在过去,如果是,则add() 一天到它。
猜你喜欢
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 2013-02-11
相关资源
最近更新 更多