在Android的特定时间呼叫服务 [英] Call service at specific time in android
问题描述
我在后台服务运行。结果
我想打电话给在特定的时间间隔这项服务。结果
如果用户提供值10,20,30比服务应10分钟,20分钟和30分钟后接受性调用。
I have service run in background.
I want to call this service at specific interval.
if user provide values 10, 20, 30 than service should call after 10 min,20 min and 30 min receptively.
我怎样才能做到上述的事情吗?
How can I do above thing?
推荐答案
AlarmManager
将帮助您:)
AlarmManager
will help you :)
它允许您设置一个时间表规定的时间范围内启动应用程序的组件
It allows you to set up a schedule to launch your application's components during the specified time range
要实例化一个 AlarmManager
它实例化后会发挥作用的特定时期, setRepeating配置()
方法和周期参数添加到 SystemClock.elapsedRealime()
:
To instantiate an AlarmManager
that will go into play at the specific period after it's instantiating, configure it with setRepeating()
method and PERIOD parameter added to SystemClock.elapsedRealime()
:
AlarmManager mgr = (AlarmManager)getSystemService(ALARM_SERVICE);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi); // Here PERIOD is a value specified by you as PendingIntent object
这篇关于在Android的特定时间呼叫服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!