2014-12-27 163 views
0

我的目标是有一个简单的程序,每X秒获取一次图像URL的内容,将其写入/ media/screensavers/Messages目录,然后解锁并重新锁定设备(Nook Simple Touch),以便显示新下载的图像。Android解锁屏幕,然后重新锁定屏幕(Nook简单触摸)

当它应该和图像下载时(我可以通过Android Studio logcat看到),警报会熄灭。问题是角落没有解锁。 (屏幕不更新)。

这里是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? 

} 

我的代码,我试图从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); 

getWindow()这个代码是RED(无法解析法)。

我试过使用context.getWindow(),但也有错误。如何从onReceiver上下文访问getWindow?

目标:通过

  1. 下载图片(完成)
  2. 写入图像目录(完成)
  3. 解锁装置
  4. 锁定devive

刷新 '屏幕保护程序' 的形象谢谢!

+0

'AlarmReceiver'没有这样的方法,你需要实现活动并启动它。 – gio 2014-12-27 20:14:21

回答

0

我不得不切换到使用一个活动作为意图,而不是接收器。

一旦我做到了,我把下面的代码在警报活动:

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); 

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