重复报警不准确

Repeated alarm is not accurate

我制作了一个应用程序,它有一个从 1 到 60 分钟不等的数字选择器,并将它连接到一个重复的警报管理器。当我试一试时,我发现它有时不准确,工作时间要么更长,要么更少。

可能是什么问题?

对于开始按钮:

startB.setOnClickListener(new OnClickListener()
     {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (startB.isChecked())
            {
            Calendar calSet = Calendar.getInstance();
            calSet.set(Calendar.MINUTE, picker2.getValue());
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);
            setAlarm(calSet);

            SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
            editor.putBoolean("toggleButton", startB.isChecked());
            editor.commit();
                timerHasStarted = true;

            }
        else
            {
            Intent intent = new Intent(getBaseContext(), MainReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);

            SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
            editor.putBoolean("toggleButton", startB.isChecked());
            editor.commit();
                timerHasStarted = false;

            }
        }  
        });

闹钟:

private void setAlarm(Calendar targetCal ) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(getBaseContext(), MainReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
            targetCal.getTimeInMillis(), 
            TimeUnit.MINUTES.toMillis(picker2.getValue()),
            pendingIntent);
}

接收者:

@Override
    public void onReceive(Context context, Intent intent) {

         MediaPlayer m=MediaPlayer.create(context, R.raw.sound);
         m.start();

    }

Android OS 能够切换闹钟以最大程度地减少唤醒和电池消耗(自 API 19)。看看here。我注意到最多有几秒钟的延迟。

可以找到关于一般警报的非常好的教程here