简体   繁体   English

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

[英]scheduled alarm to repeat every minute of the clock android

I have an app which requires a code to be executed every minute. 我有一个应用程序,需要每分钟执行一次代码。 But the issue is that the code has to be executed at every minute change of the clock. 但问题是代码必须在每一分钟的时钟变化时执行。 Which means, 意思是,

If its 12:34 then the code will execute at 12:35 and goes on. 如果它的12:34则代码将在12:35执行并继续。 But my current code works but it includes the seconds. 但我目前的代码工作,但它包括秒。 Meaning, 含义,

If its 12:34:30 and the alarm starts, the code is executed. 如果它的12:34:30并且警报开始,则执行代码。 But the code is then executed at 12:35:30. 但是代码然后在12:35:30执行。

I want the code to be executed each minute according to the clock of the phone. 我希望每分钟根据手机的时钟执行代码。 Below is the current code. 以下是当前代码。

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

Im making it execute every second so that the effect takes place at the exact time. 我让它每秒执行一次,以便在确切的时间发生效果。 Instead of having it every second i need it to repeat itself at every minute change of the clock (every minute) 而不是每一秒我都需要它在时钟的每一分钟变化(每分钟)重复一次

How do i go about this 我该如何解决这个问题

Use Intent.ACTION_TIME_TICK This is a broadcast intent fired every minute by the Android OS. 使用Intent.ACTION_TIME_TICK这是Android OS每分钟触发的广播意图。 Register to it as you would register to a normal system broadcast in code (doesn't work from manifest) 注册到它,因为您将注册到代码中的正常系统广播(从清单不起作用)

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 article describes the complete process. 文章描述了整个过程。

Use a Calendar to set the time to trigger to the next full minute, and repeat it every minute (60*1000ms) 使用日历设置触发下一整分钟的时间,并每分钟重复一次(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);

Hope this code solved your problem 希望这段代码能解决你的问题

Use the Quartz Schedular API, make the corn job which run at every min. 使用Quartz Schedular API,制作每分钟运行的玉米作业。

 “0 0/1 * * * ?” // Corn expression run at  every min

Build the Schedular and trigger like that 构建计划并像这样触发

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

and write your code in HelloJob class 并在HelloJob类中编写代码

 public class HelloJob implements Job {

  public void execute(JobExecutionContext context)
  throws JobExecutionException;

 // Here you write your code which exceute on every min
}

You can use below code for your requirement, 您可以使用以下代码满足您的要求,

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

You can use Intent.ACTION_TIME_TICK. 您可以使用Intent.ACTION_TIME_TICK。

Broadcast Action: The current time has changed. 广播行动:当前时间已经改变。 Sent every minute. 每分钟发送一次。 You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver(). 您不能通过在清单中声明的​​组件来接收它,只能通过使用Context.registerReceiver()显式注册它。

Source: https://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_TICK 资料来源: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM