如何正确使用AlarmManager类以固定时间间隔执行任务?

编程入门 行业动态 更新时间:2024-10-27 23:19:04
本文介绍了如何正确使用AlarmManager类以固定时间间隔执行任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图让这必须在固定的时间间隔执行某项任务的应用程序。大量的研究和尝试后,我想通它的更好的内存和电池使用量使用到期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(Speed​​Motors.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类以固定时间间隔执行任务?

本文发布于:2023-11-27 14:45:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   如何正确   时间   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!