0

I have an app that starts a full screen activity at a given time (based on user settings). I need to wake up the device when this happens so the user can see the activity. I use AlarmManager to schedule these events and it seems to work fine (I didn't notice any missed alarm event), however, on some devices it happens that the screen doesn't turn on. In this case if I manually wake up the device then I can see that my activity is there and it's running it just didn't turned on the screen. I can't reproduce it every time, sometimes it works, sometimes it doesn't. There are some devices that work fine all the time. I can see this problem on different OS versions so it's not specific to an SDK I guess.

An example how I set the AlarmManager:

Intent myIntent = new Intent(context, AlarmActivity.class);
myIntent.putExtras(extras);
PendingIntent pIntent = PendingIntent.getActivity(context, ID, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE); 
alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeInMillis, pIntent);

Here is what I use in onCreate of the AlarmActivity:

PowerManager pm;
WakeLock wakeLock;
WakeLock cpuWake;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    pm = (PowerManager) getSystemService(POWER_SERVICE);
    cpuWake = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    cpuWake.acquire();
    wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.FULL_WAKE_LOCK, TAG);
    if (!wakeLock.isHeld()) {
        wakeLock.acquire();
    } 

    final Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.layout);
//rest of the code

Note: I tried it without PARTIAL_WAKE_LOCK, the result was the same behavior.

Any idea what do I do wrong?

I would appreciate any comment as this problem causes me headache now.

Thank you.

drk
  • 398
  • 4
  • 13

1 Answers1

0

Well, to speed up the debugging process I decided to use one of my test devices that I was able to use to reproduce this issue. After a lot of tries I realized that even the stock clock was behaving the same way so basically it negated all my tries. Anyway I was able to reproduce it on another device (less likely).

What I ended up using (based on other SO answers) is a BroadcastReciever for my alarms and use wakelock in a static way (acquire on receive of the broadcast and release onstop of the activity). So far it seems to work.

I had another wakelock issue in a service, for that I ended up using wakefulbroadcastreceiver and startwakefulservice (for this its enough to wake the cpu).

drk
  • 398
  • 4
  • 13
  • Unfortunately, I still get reports from users that screen does not get turned on. I'm open to any ideas what should I check. Thank you. – drk Mar 28 '14 at 10:35