AlarmManager.setRepeating()不会在正确的时间重复

时间:2015-08-24 05:57:59

标签: android android-alarms

我正在尝试创建一个原型应用程序,其中AlarmManager需要重复(例如,每5秒钟)执行某些操作。它在我的BroadcastReciever的onRecieve()方法中重复执行任何代码,但它会在1分钟内重复,即使它应该每5秒重复一次。我已经按照所有教程,我很确定我遵循所有步骤。有人可以帮忙找出为什么每次都重复一次吗?

AlarmRecurring.java

    public class AlarmRecurring extends AppCompatActivity {

  private PendingIntent pendingIntent;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_alarm_recurring);
    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    setupButtons();

  }

  private void setupButtons()
  {
    findViewById(R.id.stopRepeating).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        cancel();
      }
    });
    findViewById(R.id.startRepeating).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        startrepeating();
      }
    });

  }


  public void cancel() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.cancel(pendingIntent);
    Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
  }

  public void startrepeating() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    EditText secondsToRepeat =(EditText) findViewById(R.id.secondsToRepeat);
    int repeat = Integer.parseInt(secondsToRepeat.getText().toString());

        /* Repeating on every 20 minutes interval */
    TimePicker tp = (TimePicker) findViewById(R.id.timePicker);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
    calendar.set(Calendar.MINUTE, tp.getCurrentMinute());
    manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            1000 * repeat, pendingIntent);

    Toast.makeText(this, "Repeating Alarm Started at every: " + repeat + "secs", Toast.LENGTH_SHORT).show();
  }

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
    MediaPlayer mp=null;

    @Override
    public void onReceive(Context context, Intent intent) {
        // For our recurring task, we'll just display a message
        Toast.makeText(context, "Repeating Alarm running at:" + new Date().toString(), Toast.LENGTH_SHORT).show();

        mp = MediaPlayer.create(context , R.raw.short_notice);
        mp.start();
    }
}

谢谢: - )

2 个答案:

答案 0 :(得分:0)

从API 22开始,如果给定的值较小,则间隔的值将被强制为60000 ms。

答案 1 :(得分:0)

有一些android targetsdkversion。

从API 19(KITKAT)开始报警传送不正确:

Android操作系统将移动警报,以最大限度地减少唤醒和电池使用。 Te Os已经提供了新的API来支持需要严格交付保证的应用程序;

  • setWindow(int,long,long,PendingIntent);

  • setExact(int,long,PendingIntent);

targetSdkVersion早于API 19的应用程序将继续查看之前在所请求时准确传递所有警报的行为。