[英]scheduled alarm to repeat every minute of the clock android
我有一个应用程序,需要每分钟执行一次代码。 但问题是代码必须在每一分钟的时钟变化时执行。 意思是,
如果它的12:34则代码将在12:35执行并继续。 但我目前的代码工作,但它包括秒。 含义,
如果它的12:34:30并且警报开始,则执行代码。 但是代码然后在12:35:30执行。
我希望每分钟根据手机的时钟执行代码。 以下是当前代码。
Intent intent2 = new Intent(MainActivity.this, MyABService.class);
PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, intent2, 0);
AlarmManager alarm_manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm_manager.setRepeating(AlarmManager.RTC, c.getTimeInMillis(), 1 * 1000, pintent);
我让它每秒执行一次,以便在确切的时间发生效果。 而不是每一秒我都需要它在时钟的每一分钟变化(每分钟)重复一次
我该如何解决这个问题
使用Intent.ACTION_TIME_TICK这是Android OS每分钟触发的广播意图。 注册到它,因为您将注册到代码中的正常系统广播(从清单不起作用)
tickReceiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK)==0)
{
//do something
}
};
//Register the broadcast receiver to receive TIME_TICK
registerReceiver(tickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
该文章描述了整个过程。
使用日历设置触发下一整分钟的时间,并每分钟重复一次(60 * 1000ms)
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 0);
long triggerAt = calendar.getTimeInMillis();
long repeatAfter = 60 * 1000;
alarm_manager.setRepeating(AlarmManager.RTC, triggerAt, repeatAfter, pintent);
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(context, AnyClass.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 60*1000, pendingIntent);
希望这段代码能解决你的问题
使用Quartz Schedular API,制作每分钟运行的玉米作业。
“0 0/1 * * * ?” // Corn expression run at every min
构建计划并像这样触发
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("myJob", "group1")
.build();
// Trigger the job to run now, and then every 1 min
Trigger trigger =trigger = newTrigger()
.withIdentity("trigger1", "group1")
.withSchedule(cronSchedule("0 0/1 * * * ?"))
.forJob("myJob", "group1")
.build();
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
并在HelloJob类中编写代码
public class HelloJob implements Job {
public void execute(JobExecutionContext context)
throws JobExecutionException;
// Here you write your code which exceute on every min
}
您可以使用以下代码满足您的要求,
Calendar initialCalendar = Calendar.getInstance();
initialCalendar.setTimeInMillis(System.currentTimeMillis());
initialCalendar.set(Calendar.SECOND, 0);
long triggerAtMillis = initialCalendar.getTimeInMillis();
long intervalMillis = 60 * 1000;
alarm_manager.setRepeating(AlarmManager.RTC, triggerAtMillis, intervalMillis, pintent);
您可以使用Intent.ACTION_TIME_TICK。
广播行动:当前时间已经改变。 每分钟发送一次。 您不能通过在清单中声明的组件来接收它,只能通过使用Context.registerReceiver()显式注册它。
资料来源: https : //developer.android.com/reference/android/content/Intent.html#ACTION_TIME_TICK
IntentFilter tickIntent = new IntentFilter(Intent.ACTION_TIME_TICK);
private BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context c, Intent i) {
// perform per minute task
}
};
registerReceiver(receiver, tickIntent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.