我已经搜索了很多地方,但无法找到如何开始的服务的清洁顺序解释(或者如果多数民众赞成不可能的,那么一个活动),在使用AlarmManager特定的时间每天?
我要注册几个这样的报警和触发他们应该导致服务启动。 我会是一种在服务一小块的代码,然后可以执行,我可以为完成好服务....
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!!");
我想这在代码4.45激活报警......但它不是射击服务...我一定要保持进程运行? Mi个做错了什么???
还有一两件事,我的服务的情况下,我用下面的代码被执行的完美:
long firstTime = SystemClock.elapsedRealtime();
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 30*1000,pintent);
Answer 1:
喜的朋友,很多研发的经过,并与同一主题从“Pentium10”的问题,参考我设法得到它的工作。 虽然我仍然无法理解为什么“日期”的理念,日历,我在这个问题提到(非的GregorianCalendar)对象无法正常工作。
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);
Answer 2:
//Create alarm manager
AlarmManager alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create pending intent & register it to your alarm notifier class
Intent intent0 = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0, intent0, 0);
//set timer you want alarm to work (here I have set it to 7.20pm)
Intent intent0 = new Intent(this, OldEntryRemover.class);
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 19);
timeOff9.set(Calendar.MINUTE, 20);
timeOff9.set(Calendar.SECOND, 0);
//set that timer as a RTC Wakeup to alarm manager object
alarmMgr0.set(AlarmManager.RTC_WAKEUP, timeOff0.getTimeInMillis(), pendingIntent0);
然后在您的AlarmReciever类,这是一个broadcastReciever,下onRecieve方法把你的逻辑。 这会照顾你希望在时机成熟时7.20点至处理什么都逻辑。
如果您需要设置多个闹钟,创建另一个日历实例及适当地设置时间值。 您还需要创建的PendingIntent另一个实例,否则定时器就会重叠。 然后将其设置为与新的定时器及的PendingIntent相同alarmManager。
Answer 3:
您可以读取文件https://developer.android.com/training/scheduling/alarms.html
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
//设置闹钟在早上8:30开始
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating()允许你指定一个精确的自定义间隔 - 在这种情况下,// 20分钟。
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
Answer 4:
下面的代码应该可以正常工作,并启动该服务@每天7:40 PM。 另外,如果设备关闭,那么所有的警报被取消。
确保系统启动完毕后设立的所有报警。
Intent slIntent = new Intent(this, x.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 0);
PendingIntent slPendingIntent = PendingIntent.getService(this, 1, slIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, slPendingIntent);
Answer 5:
我尝试了很多启动服务的时间,所以我有这样一个解决方案
计算当前时间之间以及选定的时间从日期选择器“返回龙timeMileSec =毫秒”上的不同 :
在这之后创建里面的处理程序和睡觉,如果“毫秒”秒
new Handler().postDelayed(new Runnable() { @Override public void run() { CreateService(); getActivity().startService(intentServiceObj); } }, timeMileSec); // Below is the service Methods. private void CreateService() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); cal.add(Calendar.DAY_OF_YEAR, year); cal.set(Calendar.HOUR_OF_DAY, hourScreen); cal.set(Calendar.MINUTE, minuteScreen); // cal.setTimeInMillis(timeselectedmillisecond); Intent intent = new Intent(getActivity(), ServiceDailyLocationUpdate.class); pintent = PendingIntent.getService(getActivity(), 0, intent, 0); alarm = (AlarmManager) getActivity().getSystemService( Context.ALARM_SERVICE); alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.HOUR, 86000 * 1000, pintent); } // Return Differnce between two date private long calculateDateDiffSecond(String firstDate, String secondDate) { long numberOfDays = 0; String dateStart = firstDate; String dateStop = secondDate; Date d1 = null; Date d2 = null; try { d1 = sdfTime.parse(dateStart); d2 = sdfTime.parse(dateStop); // in milliseconds long diff = d2.getTime() - d1.getTime(); long diffSeconds = diff / 1000 % 60; long diffMinutes = diff / (60 * 1000) % 60; long diffHours = diff / (60 * 60 * 1000) % 24; long diffDays = diff / (24 * 60 * 60 * 1000); System.out.print(diffDays + " days, "); System.out.print("Hours::" + diffHours + " hours, "); System.out.print("HoursMinute::" + diffMinutes + " minutes, "); System.out.print(diffSeconds + " seconds."); numberOfDays = diffDays; numberOfDays = diff; } catch (Exception e) { e.printStackTrace(); } return numberOfDays; }
Answer 6:
上面的代码没有工作,下面的代码为我工作。
一个月减少1和小时0-11。
int day = ff.getGregorianDay() ;
int month = ff.getGregorianMonth() ;
int year = ff.getGregorianYear();
int hour = TimePicker1.getCurrentHour();
int minute = TimePicker1.getCurrentMinute();
Calendar cal_alarm = Calendar.getInstance();
cal_alarm.setTimeInMillis(System.currentTimeMillis() );
cal_alarm.set(Calendar.YEAR, year);
cal_alarm.set(Calendar.MONTH, month-1);
cal_alarm.set(Calendar.DAY_OF_MONTH, day);
cal_alarm.set(Calendar.AM_PM, Calendar.AM );
cal_alarm.set(Calendar.HOUR, hour);
if( hour >= 12){
cal_alarm.set(Calendar.AM_PM, Calendar.PM );
cal_alarm.set(Calendar.HOUR, hour-12);
}
cal_alarm.set(Calendar.MINUTE, minute );
cal_alarm.set(Calendar.SECOND, 0 );
Intent myIntent = new Intent(YadavariNewActivity.this, Alarm_Sag.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(YadavariNewActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set( AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);
文章来源: Using Alarmmanager to start a service at specific time