Android的运行一些code在特定的时间,但只有一次 [英] Android run some code at specific time but only once

查看:115
本文介绍了Android的运行一些code在特定的时间,但只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在18:22,但只运行一次在特定的时间约code为例。

I want to run some code at specific time, for example at 18:22 but only once.

我尝试一起工作:

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

是很好用:

alarmManager.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);

或者是更好地使用处理程序来在特定时间运行特定的code?

or is better to use handler to run specific code at specific time?

推荐答案

这是更好地使用AlarmManager这样的precise计时。

It is better to use AlarmManager for such precise timings.

通过处理器将需要您的应用程序保持活动的全部时间,从而可以对设备的电池带来负面影响。在另一方面,AlarmManager会时所需的时间来启动你的应用程序,你不需要保持运行的全部时间。

Using a handler will require your app to stay active the whole time, which can have a negative effect on the device's battery. On the other hand, AlarmManager will start up your app when the required time comes, and you don't need to keep it running the whole time.

这篇关于Android的运行一些code在特定的时间,但只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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