java - 重复报警不准确

标签 java android eclipse alarmmanager

我制作了一个应用程序,它有一个从 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 操作系统能够转移闹钟,以最大限度地减少唤醒和电池消耗(自 API 19 起)。看看here .我注意到最多有几秒钟的延迟。

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

关于java - 重复报警不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30870188/

相关文章:

Windows 8 上的 Eclipse 兼容性

eclipse - eclipse.ini 文件中的环境变量

java - JTable 当前显示多少行?

java - 为什么文件重命名在 Java 中会失败

java - 构建失败[错误] 15669

java - 通过调用另一个类中引用另一个方法的方法来打印排序数组 Java Eclipse

android - 如何在 Android 中高效地加载重负载的 ListView [没有图像,位图]

java - 如何从这样设置的 ListView 中删除列表项

android - Android 浏览器不支持带百分比值的边框半径

java - 为什么我们不再需要在 Eclipse 中在类路径中设置带有 JNI 实现的库?