在特定时间执行函数
我想在 TimePicker 选择的特定时间执行某些功能。
我可以像这样使用处理程序:
Handler myHandler = new DoSomething();
Message m = new Message();
myHandler.sendMessageDelayed(m, delay);
class DoSomething extends Handler {
@Override
public void handleMessage(Message msg) {
MyObject o = (MyObject) msg.obj;
//do something here
}
}
并加载“延迟”参数:picked_wanted_hour - current_hour 或者以一定的时间间隔检查当前时间,并在时间到来时调用该函数。
但是,是否有任何类似处理程序的对象可以花费特定时间并在那时调用操作?
提前致谢。
I want to execute some function at specific hour picked by TimePicker.
I can use Handler like this:
Handler myHandler = new DoSomething();
Message m = new Message();
myHandler.sendMessageDelayed(m, delay);
class DoSomething extends Handler {
@Override
public void handleMessage(Message msg) {
MyObject o = (MyObject) msg.obj;
//do something here
}
}
And load the "delay" param with: picked_wanted_hour - current_hour
OR check for the current time at some interval and invoke the function when the time comes.
But, is there any handler-like object that can take a specific time and invoke the operation at that time?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 AlarmManager 服务:
http://developer.android.com/reference/android /app/AlarmManager.html
简要示例:
有关 PendingIntent 的更多信息
http://developer.android.com/reference/android/app/PendingIntent.html
You can use AlarmManager service :
http://developer.android.com/reference/android/app/AlarmManager.html
A bref sample:
more infos on PendingIntent
http://developer.android.com/reference/android/app/PendingIntent.html
为什么不使用处理程序本身?只需传递
timePickerTime - currentTime
作为延迟即可。还有 Timer 方法
schedule(TimerTask task, Date when )
。Why don't you use handler itself? Just pass
timePickerTime - currentTime
as a delay.There's also Timer with method
schedule(TimerTask task, Date when)
.