预定闹钟重复时钟android的每一分钟

时间:2016-06-29 10:03:57

标签: java android alarmmanager

我有一个应用程序,需要每分钟执行一次代码。但问题是代码必须在每一分钟的时钟变化时执行。这意味着,

如果是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);

我让它每秒执行一次,以便效果在确切的时间发生。而不是每一秒都有它,我需要它在时钟的每一分钟变化(每分钟)重复一次

我如何解决这个问题

6 个答案:

答案 0 :(得分:10)

使用Intent.ACTION_TIME_TICK这是Android操作系统每分钟发出的广播意图。注册到它,因为您将注册到代码中的正常系统广播(不能从清单中工作)

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));

This文章描述了整个过程。

答案 1 :(得分:3)

使用日历设置触发下一整分钟的时间,并每分钟重复一次(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);

答案 2 :(得分:0)

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);

希望此代码解决您的问题

答案 3 :(得分:0)

使用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
}

答案 4 :(得分:-1)

您可以使用以下代码满足您的要求,

    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);

答案 5 :(得分:-1)

您可以使用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);