Android Unlock screen, then relock screen (Nook Simple Touch)

264 Views Asked by At

My goal is to have a simple program which fetches the contents of an image URL every X seconds, writes it to the /media/screensavers/Messages directory, then unlocks and relocks the device (Nook Simple Touch) so the new downloaded img is displayed.

The alarm fires off when it should and the image downloads (I can see via Android Studio logcat). The problem is the nook does not unlock. (the screen does not update).

Here is my code from AlarmReceiver.java

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

    // Log to logcat
    Log.i("AlarmReceiver", "onReceive() -- onReceive fired! ");

    // Create the dummy image url
    String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
    String imgPath = new String();
    imgPath = "http://dummyimage.com/600x800/fff/000.jpg&text=" + currentDateTimeString ;
    imgPath = imgPath.replaceAll(" ", "+");

    // Download and save the image (works great)
    new DownloadImageTask().execute(imgPath);

    // Now how do I a) unlock the device, then b) put it back to sleep?

}

I tried this code from How to programmatically dismiss the screensaver/lock screen on Android (Nook Simple Touch)

Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

but getWindow() is RED (Can't resolve method).

I've tried using context.getWindow(), but that also has errors. How do I access getWindow from within the onReceiver context?

Goal: Refresh the 'Screensaver' image via

  1. Download Image (done)
  2. Write image to directory (done)
  3. Unlock device
  4. Lock devive

Thanks!

1

There are 1 best solutions below

0
On

I had to switch to using an Activity as the intent, rather than a receiver.

Once I did that, I put the following code in the alarm activity:

KeyguardManager km = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
        final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("MyKeyguardLock");
        kl.disableKeyguard();

        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);

        PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
        wakeLock.acquire(1000);

The above code unlocks the Nook! Note that the getWindow() method with flags does not seem to work on the nook touch. Not sure why.