如何在Android的日常特定时间做某事

时间:2014-09-25 21:10:25

标签: java android

我想每天早上9点检查一些网址。但我怎么能这样做?我确实检查了一些关于AlarmManager和IntentService的主题和文章,但是我无法为我的应用程序聚在一起,是否有任何示例代码或什么?谢谢..

1 个答案:

答案 0 :(得分:2)

试试这个:

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