Repeat signal inaccurate

I made an application that has a dial to select a number from 1 to 60 minutes, and I connected it to the re-dispatch alarm. When I tried, I noticed that sometimes it’s not so accurate, it takes more minutes to work.

What could be the problem?

For the start button:

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;

            }
        }  
        });

For anxiety:

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

Recipient:

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

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

    }
+4
source share
1 answer

Android can switch alarms to minimize wake-ups and battery consumption (with API 19). Take a look here . I noticed delays of up to several seconds.

Here you can find a pretty good alarms tutorial in general here

+6
source

All Articles