N
N
Nurshat2016-05-09 19:46:04
Java
Nurshat, 2016-05-09 19:46:04

How to guarantee the operation of a self-written alarm clock?

Hmm ... I don’t know how to invent headlines (
Bottom line:
I wrote something like an alarm clock, and at first glance everything was fine and everything worked, but later I discovered one bug, namely, this alarm clock did not work when the phone screen was off. How everything is done: there is a service in which a timer is started which, at intervals of a minute, checks whether the specified time has come and whether it needs to "work" - and this works, but only until about a minute has passed since the screen was turned off. this very minute, the alarm stops working, and will only work if you unlock the phone.
And there is one interesting point: if you connect the phone to the computer and start reading the logs of the locked phone with the alarm running, everything works after half an hour and after an hour.
timer initialization:

nearestTime = changeDB.getNearestSimpleAlarmTime();
        if (mTimer != null) {
            mTimer.cancel();
            mTimer.purge();
            mTimer = null;
        }
        if (nearestTime.getHour() > -1) {
            calendar = Calendar.getInstance();
            int seconds = 60 - calendar.get(Calendar.SECOND);
            System.out.println("первая проверка через " + seconds + " секунд");

            mTimer = new Timer();
            timerTask = new SimpleTimer(nearestTime, getApplicationContext());
            mTimer.schedule(timerTask, seconds * 1000, 60000);
        } else {
            stopSelf();
        }

timer body, run method:
calendar = Calendar.getInstance();
        int minute = calendar.get(Calendar.MINUTE);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        if (nearestTime.getHour() < hour || (nearestTime.getHour() == hour && nearestTime.getMinute() < minute) || (nearestTime.getHour() == hour && nearestTime.getMinute() == minute)) {
            Intent scheduledIntent = new Intent(context, AlarmActivity.class);
            scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            scheduledIntent.putExtra("hour", nearestTime.getHour());
            scheduledIntent.putExtra("minute", nearestTime.getMinute());
            scheduledIntent.putExtra("fulltime", nearestTime.getFulltime());
            context.startActivity(scheduledIntent);
        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis Zagaevsky, 2016-05-10
@Nurshat

This is absolutely the wrong approach to the alarm clock.
Should be done using AlarmManager . In particular, on Android 6+, setAndAllowWhileIdle() or setExactAndAllowWhileIdle() should be used .
The BroadcastReceiver that will be used with the AlarmManager should inherit from WakefulBroadcastReceiver so that the device does not fall asleep during the alarm. The sound must be played in the foreground Service (you have already mastered it).
Also, I recommend reading about Doze mode .

G
GavriKos, 2016-05-09
@GavriKos

Try adding permission

<uses-permission android:name="android.permission.WAKE_LOCK"/>

D
Dmitry Alexandrov, 2016-05-13
@jamakasi666

Today, many manufacturers of smartphones on android are very fond of rewriting pieces of android. For example, I will give emui firmware in which there is a tricky taskkiller. It sends the program to the deepest sleep and slowly accumulates all the events for the program in the queue, then the program wakes up when the screen is turned on or it starts, and the entire queue of events that has accumulated is poured into the program.
It looks terrible from the side of application development, but from the side of the user it's cool because it saves battery power wildly. Perhaps you have a similar case and just the Chinese / Indians cheated in the firmware?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question