2016-06-29 83 views
6

我有一个应用程序需要每分钟执行一次代码。但问题是代码必须在每分钟更改一次的时间内执行。也就是说,预定闹钟重复时钟的每一分钟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); 

林使其每秒执行一次,这样的效果在准确的时间发生。而不是它的每一秒我需要它重演在(每分钟)

时钟的每分钟变化我如何去了解这个

+2

如果你只需要这个您的应用程序运行时,屏幕上,使用'ScheduledExecutorService'等进程内解决方案。如果你正在寻找这个工作,即使你的应用程序的进程被终止或屏幕没有打开,放弃和写一些其他的应用程序,因为Android不支持Android 6.0 +的情况。 – CommonsWare

回答

3

使用日历设置时间触发到下一个全分钟,重复每一分钟(60和* 1000毫秒)

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

那么这个setRepeating被调用的时候它会被执行吗?就像如果闹钟设置在11:23那么它也会在11:23执行,或者它只会在11:24执行 – AxeManTOBO

+0

这样它会在下一个整分钟触发,所以11.24 –

+0

如果你想开始在11.23,您将calendar.minute设置为0而不是添加1 –

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

希望这段代码解决您的问题

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本文介绍的完整过程。

+0

工作这个解决方案,即使设备处于打盹模式? – cpalosrejano

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

感谢您的重复我的回答 –

-1

您可以使用Intent.ACTION_TIME_TICK。

广播动作:当前时间已更改。每分钟发送一次。您无法通过在manifest中声明的组件接收此消息,只能通过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); 
+0

这已经提前一天回答 –

+0

我的错误@TimCastelijns – 100rabh

0

使用石英程序器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 
}