如何正确使用AlarmManager类以固定时间间隔执行任务? [英] How to correctly use the AlarmManager class to execute a task at fixed intervals?
问题描述
我试图让这必须在固定的时间间隔执行某项任务的应用程序。大量的研究和尝试后,我想通它的更好的内存和电池使用量使用到期AlarmManager类,其效率,而不是运行服务内的计时器。
有一些方面,现在有问题I'me。结果
一个是我应该如何访问到正在被一个previous运行中创建,以便能够取消报警对象?
意向意图=新意图(getActivity(),MyService.class);
PendignIntent pintent = PendingIntent.getService(getActivity(),0,意向,0);
AlarmManager报警=(AlarmManager)getActivity()getSystemService(Context.ALARM_SERVICE)。
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,SystemClock.elapsedRealtime()+
20000,AlarmManager.INTERVAL_FIFTEEN_MINUTES,pintent);
有办法知道如果报警已设置或没有,但它不能给你报警本身。
布尔alarmUp =(PendingIntent.getService(getActivity(),0,新的意图(getActivity(),MyService.class),PendingIntent.FLAG_NO_CREATE)!= NULL);
如果(alarmUp){
//如何取消报警
}其他{
Toast.makeText(getActivity(),报警未运行,Toast.LENGTH_SHORT).show();
}
是遵守前面取消基于类似的PendingIntent这 - 报警
AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE); 意图alarmintent1 =新意图(MainActivity.this,AlarmReceiver.class);
的PendingIntent sender1 = PendingIntent.getBroadcast(SpeedMotors.this,100,alarmintent1,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); 尝试{
am.cancel(sender1);
}赶上(例外五){
// TODO自动生成catch块
e.printStackTrace();
的System.out.println(EX .....+ E);
} 日历CAL = Calendar.getInstance();
cal.add(Calendar.SECOND,5);
am.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),1000 * 10,sender1);
的System.out.println(报警设置...........);
试一试来帮你。
I'm trying to make an app which must execute a certain task at fixed intervals. After much research and trying I figured it's better to use the AlarmManager class due to its efficiency at memory and battery usage instead of running a timer inside a service.
There are some aspects now that I'me having problem with.
One is how should I get access to the alarm object that is been created on a previous run in order to be able to cancel it?
Intent intent = new Intent(getActivity(), MyService.class);
PendignIntent pintent = PendingIntent.getService(getActivity(), 0, intent, 0);
AlarmManager alarm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() +
20000, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pintent);
There is way to know if the alarm has been set or not but it can't give you the alarm itself.
boolean alarmUp = (PendingIntent.getService(getActivity(), 0, new Intent(getActivity(), MyService.class), PendingIntent.FLAG_NO_CREATE) != null);
if(alarmUp){
//How to cancel the alarm
}else {
Toast.makeText(getActivity(), "Alarm not running", Toast.LENGTH_SHORT).show();
}
Yes youcan cancel alarm based on PendingIntent like this-
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
Intent alarmintent1=new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent sender1=PendingIntent.getBroadcast(SpeedMotors.this, 100, alarmintent1, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
try {
am.cancel(sender1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ex....."+e);
}
Calendar cal=Calendar.getInstance();
cal.add(Calendar.SECOND, 5);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*10, sender1);
System.out.println("alarm set...........");
try it ll help you.
这篇关于如何正确使用AlarmManager类以固定时间间隔执行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!