dor506
dor506

Reputation: 5404

Turn on / Wake screen for a period of time

Im trying to implement something -

the screen is off (dark). I want to be able to wake the screen for a period of 15 seconds (by an event/broadcast or something...) , and after that, if there was no intervention of the user, the screen should be off (dark) again.

How can i do this?

Upvotes: 1

Views: 1103

Answers (2)

mwengler
mwengler

Reputation: 2778

You can use an AlarmManager.

You trigger the alarms with lines like this:

Alarm Manager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

pendingServiceIntent = PendingIntent.getService(this.getApplicationContext(), 0,
            new Intent(this.getApplicationContext(), DataCollectionService.class), 0);

long intervalInMinutes = 5; // will wake you up every 5 minutes
long triggerAtTime = System.currentTimeMillis() + 1000*60*intervalInMinutes;
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pendingServiceIntent);

In my case I was triggering a service. You can extend a BroadcastReceiver or whatever you want. You will then use a WakeLock to light up the screen for at least 15 seconds:

powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wL = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "LocRepTask");
wL.acquire(); // forces processor to stay awake
// do your stuff.....
wl.release();  // processor no longer awake because of you

You will need permission WAKE_LOCK in android manifest.

Upvotes: 2

JRaymond
JRaymond

Reputation: 11782

In order to do this, you would have to have a running Service holding a partial wake lock. The service could then fire off an intent to a broadcastreceiver at whatever interval you want and wake the screen. Like the commenter suggested, though, this means that the CPU has to stay on, even when the device screen is off, and this will drain the battery faster than standby would. (which by itself is not a reason not to do it, just saying you have to weigh the consideration)

The other requirements you have are easily covered with the PowerManager API - hopefully that's enough for you to go off of. good luck!

Upvotes: 1

Related Questions