0

Hi and thanks for your help.

I have the following situation.

I have a Lock Screen Widget, when the user taps it the App Widget perform some tasks (updates itself).

The point is that: if the phone is in sleep mode and the user taps the App Widget, the App Widget would update itself, but the App Widget itself is not visible.

Therefore I need to wake up the device when the user taps the Lock Screen Widget - and after it can go to sleep again :-) -

Therefore I could use:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();

In the AppWidgetProvider. The point is: how do I call "release()" so that the device can go back to sleep?

If I do:

    PowerManager pm = (PowerManager) ctxt.getSystemService(Context.POWER_SERVICE);
    WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
    wl.acquire();
    wl.release();

in the AppWidgetProvider simply the device never wakes up.

Any suggestion more than welcome!

Thanks!!

Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • how can user can taps the app widget when the phone is in sleep mode? the screen is locked isn't it? – Niko Adrianus Yuwono Jun 04 '13 at 15:06
  • @NAYOSO yes, youre right, but he can tap a Widget on the Lock Screen before the screen gets locked (and gets gray before locking). Anyway I need to keep it awake as long as he continues tapping on the Widget... – Lisa Anne Jun 04 '13 at 15:16
  • wake lock from power manager should do the task but I don't know if it's working in from the app widget (outside app activity) or not – Niko Adrianus Yuwono Jun 04 '13 at 15:19
  • and I think it's not allowed because it'll drained the device battery if someone can make the screen state always on outside the apps, but it's just my thinking CMIIW, I'll post an answer if I have another information about this :) – Niko Adrianus Yuwono Jun 04 '13 at 15:21

1 Answers1

0

Well, I used a Handler to call

wl.release()

after 60 seconds:

public class AppWidget extends AppWidgetProvider {

@Override
public void onUpdate(Context ctxt, AppWidgetManager mgr, int[] appWidgetIds) {
    ComponentName thisWidget = new ComponentName(ctxt, AppWidget.class);
    int[] allWidgetIds = mgr.getAppWidgetIds(thisWidget);
    Intent i = new Intent(ctxt, UpdateService.class);
    i.putExtra("widgetsids", allWidgetIds);
    ctxt.startService(i);

    PowerManager pm = (PowerManager) ctxt
            .getSystemService(Context.POWER_SERVICE);
    final WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
            | PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
    wl.acquire();
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            wl.release();
        }
    }, 60000);

}
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157