如何安排服务在Android上运行 [英] How to schedule a service to run in android

查看:169
本文介绍了如何安排服务在Android上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序,使一些API请求,并使用在响应中提供的数据服务,有没有办法安排在每个小时运行服务
谢谢

I have a Service in my app that make some API request and use the data supplied in the response, is there a way to schedule the service to run in every hour Thank you

推荐答案

这就是我在最后做到了:

that's how i did it at the end :

Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, ProximityAlertService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Log.d("Main",String.valueOf( cal.getTimeInMillis()));
//make the alarm goes off every 10 sec (not exact help to save battery life)
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 10000, pintent);

这篇关于如何安排服务在Android上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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