How to turn the screen off?

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

How to turn the screen off?

Postby StephenLe » Wed Jun 24, 2009 2:32 am

we need to control the screen off/on...Anyone know the API to implement it? Give me a hand...
User avatar
StephenLe
Freshman
Freshman
 
Posts: 5
Joined: Thu Mar 12, 2009 7:55 am
Location: Beijing China

Top

Postby Oleg » Mon Nov 02, 2009 9:27 pm

Hi.
Try this snippet:
@Override
protected void onCreate(Bundle savedInstanceState) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotDimScreen");
}
Also in manifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />

Refer to http://developer.android.com/reference/ ... nager.html
Oleg
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Wed Nov 28, 2007 1:49 pm
Location: Ukraine

Postby Pr0v0dn1k » Thu Nov 19, 2009 9:19 am

Don't forget to make:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. wl.release();
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
after you have finished.

Or, you may also wake up your device whenever you want with auto-release by:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. PowerManager pm = (PowerManager) getSystemService (Context.POWER_SERVICE);
  2.  
  3. pm.userActivity(SystemClock.uptimeMillis()+ SCREEN_TIMEOUT, false);
  4.  
  5.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

where the SCREEN_TIMEOUT is the timeout when the wake lock will be released (i.e. total time before sleep is SCREEN_TIMEOUT + default device screen timeout .
Pr0v0dn1k
Freshman
Freshman
 
Posts: 6
Joined: Sun Nov 08, 2009 4:11 pm

Postby i4nc4mp » Sat Jan 02, 2010 7:04 am

thanks for that snippet, it is very useful as the method I had been going by relies on a somewhat over-complex broadcast receiver!
i4nc4mp
Developer
Developer
 
Posts: 39
Joined: Sat Dec 12, 2009 6:44 pm

Postby i4nc4mp » Mon Jan 11, 2010 5:42 am

quick followup. I've been trying to get this working consistently. I want to have the device sleep quicker than the timeout settings. this is because i'm calling up an activity that disables keyguard at screen off. so any key will make the screen wake back up. there's no way to stop that from happening but i am confident I could get it to turn the screen back off faster than than the default timeout. My code has certain keys trigger the shorter timeout, and others call finish on the replacement lockscreen activity. I had an implementation of this working at one point just with wakelocks and manual releases, but it no longer seems to be working.
i4nc4mp
Developer
Developer
 
Posts: 39
Joined: Sat Dec 12, 2009 6:44 pm

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 14 guests