使用Alarmmanager开始在特定时间的服务 [英] Using Alarmmanager to start a service at specific time

查看:162
本文介绍了使用Alarmmanager开始在特定时间的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻觅了很多地方,但无法找到如何启动服务一个干净的顺序解释(或者如果多数民众赞成不可能的,那么一个活动)使用AlarmManager ??在每天的特定时间

我要注册几个这样报警器和触发它们应该导致服务启动。我将有一小片code在服务,可以执行,我可以良好的完成服务......

 日历CAL = Calendar.getInstance();
日历cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis的());
日期日期=新的日期(cur_cal.get(Calendar.YEAR),cur_cal.get(Calendar.MONTH),cur_cal.get(Calendar.DATE),16,45);
cal.setTime(日期);
意向意图=新的意图(ProfileList.this,ActivateOnTime.class);
intent.putExtra(PROFILE_ID,2);
PendingIntent pintent = PendingIntent.getService(ProfileList.this,0,意向,0);
AlarmManager报警=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
警报。设置(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pintent);
的System.out.println(设定的报警!);
 

我想这code激活警报在4.45 ...但它不发射服务......我一定要保持运行的进程? M I做错什么???

还有一件事,我的服务获取的情况下完美地执行我用下面的code:

 长firstTime = SystemClock.elapsedRealtime();
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,30 * 1000,pintent);
 

解决方案

喜的朋友, 大量的研发基准,并与Pentium10的问题,关于同一主题后,我设法得到它的工作。虽然我仍然无法理解为什么我刚才提到的问题,约会的概念和日历(非的GregorianCalendar)对象无法正常工作。

 日历cur_cal =新的GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis的()); //设置当前时间和日期本日历

日历CAL =新的GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR,cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY,18);
cal.set(Calendar.MINUTE,32);
cal.set(Calendar.SECOND,cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND,cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE,cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH,cur_cal.get(Calendar.MONTH));
意向意图=新的意图(ProfileList.this,IntentBroadcastedReceiver.class);
PendingIntent pintent = PendingIntent.getService(ProfileList.this,0,意向,0);
AlarmManager报警=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),30 * 1000,pintent);
 

I have searched a lot of places but couldnt find a clean sequential explanation of how to start a service (or if thats not possible then an activity) at a specific time daily using the AlarmManager??

I want to register several such alarms and triggering them should result in a service to be started. I'll be having a small piece of code in the service which can then execute and i can finish the service for good....

Calendar cal = Calendar.getInstance();
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Date date = new Date(cur_cal.get(Calendar.YEAR), cur_cal.get(Calendar.MONTH), cur_cal.get(Calendar.DATE), 16, 45);
cal.setTime(date);
Intent intent = new Intent(ProfileList.this, ActivateOnTime.class);
intent.putExtra("profile_id", 2);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);
System.out.println("The alarm set!!");

i tried this code to activate the alarm at 4.45... but its not firing the service... do i have to keep the process running?? M i doing anything wrong???

One more thing, my service gets perfectly executed in case i use the following code:

long firstTime = SystemClock.elapsedRealtime();
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 30*1000,pintent);

解决方案

HI friends, After a lot of researching and with reference from "Pentium10"'s question on the same topic i managed to get it working. Though i still cant understand why the "date" concept and the Calendar(non GregorianCalendar) object which i have mentioned in the question are not working correctly.

Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 32);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(ProfileList.this, IntentBroadcastedReceiver.class);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);

这篇关于使用Alarmmanager开始在特定时间的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆