1

I have an alarm application. I generally know the lifecycle of the receiver and how to use WakeLock.

Today however I was contacted by an user that have sent me a really strange log and complained that his alarm hasn't started until he have had unlocked the phone by himself. I used to have problems with phones going back to sleep after receiver completed its work and before activity was started, but creating WakeLock in the receiver seemed to fix the problem. At least until today - from log it seems that onReceive method wasn't called at all until user has unlocked phone by himself.

Facts:

  • it is the first case I have heard of
  • it has happened a few times to the user, but not every time
  • log is prepared by adding text to SQLite database. It doesn't seem to delay application in any significant way
  • infomation from onReceive was recorded over 100 seconds after expected alarm start time. It is the first method call in onReceive
  • alarm was started just after user has unlocked the phone
  • I use AlarmManager.RTC_WAKEUP flag
  • user says he doesn't have any custom rom. I wait for answer if he has any custom/special lockscreen
  • phone model is Sony Xperia U ST25A, Android 4.0.4

Any ideas what could be causing this problem? Is it possible that BroadcastReceiver's "inside" WakeLock doesn't work somehow?

EDIT:

I would like to emphasize the issue here - BroadcastReceiver should keep phone awake during its whole onReceive method. However in my case, it is either that

  • phone falls to sleep before onReceive methods end (even before finishing "logging call")
  • phone is not awaken by receiver at all

Also, I would like to point out the fact that user has stated clearly - alarm has started precisely when he has unlocked phone by himself. Couple of times.

Some code:

@Override
public void onReceive(Context context, Intent intent) {
    Logger.initialize(context, "AlarmReceiver");
    ...
}

Logger methods:

public synchronized static void initialize(Context context, String text) {
    try {
        if (mInstance == null) { // this is the block that is runned
            BugSenseHandler.initAndStartSession(context, BUGSENSE_ID);
            mInstance = new Logger(context);
            log("== Logger initialized == from "
                    + (text != null ? text : "")); // it stores times as well. Said 
                                                   // that alarm was started over 100 
                                                   // seconds after it should
        } else {
            log("logger initialized again from "
                    + (text != null ? text : ""));
        }
    } catch (Exception e) {
        try {
            BugSenseHandler.sendException(e);
            mInstance = null;
        } catch (Exception e2) {
        }
    }
}
Koger
  • 1,783
  • 2
  • 23
  • 34
  • No-one will be able to give you any proper help without some code. – Stefan de Bruijn Jan 09 '13 at 15:22
  • @StefandeBruijn I have some code as well with explanation as to way I believe it is not connected to problems cause – Koger Jan 10 '13 at 08:56
  • Dear, see my answer at this post http://stackoverflow.com/questions/20113161/start-activity-screen-even-if-screen-is-locked-in-android/31772607#31772607 – Junaid Aug 02 '15 at 14:01

1 Answers1

0

Take a look at WakefulIntentService from Commonsware

EJK
  • 12,332
  • 3
  • 38
  • 55
  • that looks interesting, but I would still like to know why phone falls to sleep before finishing onReceive, while it should stay awake until its end, at least according to documentation – Koger Jan 10 '13 at 09:01
  • OK, I see your point now. The WakefulIntent service class is useful if you spawn work outside of your broadcast receiver, which appears not to be the case... I also agree with you that according to Android documentation, AlarmManager holds a wake lock that ensures your onReceive method will complete. This sounds to me more like a case of AlarmManger not triggering on schedule. Of course some Log messages could help to clarify this. – EJK Jan 10 '13 at 15:44