0

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!

4

1 回答 1

0

我不得不切换到使用 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);

上面的代码解锁了Nook!请注意,带有标志的 getWindow() 方法似乎不适用于角落触摸。不知道为什么。

于 2015-01-03T18:41:20.930 回答