在Android中安排任务的最佳方法是什么? [英] What is the best way to schedule task in android?

查看:97
本文介绍了在Android中安排任务的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目要求是在准确的时间非常频繁地安排多个任务,延迟在0-5秒之间. 为此目的安排任务的最佳方法是什么?以下是我考虑的选项: 1.警报管理器:使用警报管理器,我观察到警报不会安排在5秒以下.即使我经过了3秒的时间,警报也会在5秒后广播. 下面是我的代码:

My project requirement is to schedule multiple tasks very frequently at exact time with delay between 0-5 seconds. What is the best way to schedule tasks for this purpose? Below are the options I considered: 1. Alarm Manager : Using Alarm Manager I have observed alarm does not schedule below 5 seconds. Even if I am passing 3 seconds time, alarm broadcasts after 5 seconds. Below is my code :

int requestCode = 101;
int delayTime = 3000L;

Intent intent = new Intent(context, TaskBroadcastReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent , PendingIntent.FLAG_ONE_SHOT);

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, delayTime, pendingIntent);

  1. Job Scheduler:我已经读过这种方法不适用于关键任务和精确的时间安排.而且我需要对关键任务进行准确的警报调度.

请同时指出其他选项.

推荐答案

我发现的最佳方法是使用处理程序.并使用postAtTime(Runnable r,long uptimeMillis)方法安排任务.

The best way I found is using Handler. And schedule tasks using postAtTime(Runnable r, long uptimeMillis) method.

有关更多信息,请参见: http://developer.android.com/reference/android/os/Handler.html

For more info see : http://developer.android.com/reference/android/os/Handler.html

这篇关于在Android中安排任务的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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