Screen Always On, PowerManager, WakeLock, Backlight

Quickly share your Android Code Snippets here...

Screen Always On, PowerManager, WakeLock, Backlight

Postby plusminus » Thu Oct 09, 2008 2:38 am

Required Permission:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.WAKE_LOCK" />
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.os.PowerManager;
  5.  
  6. public class MyActivity extends Activity {
  7.  
  8.         protected PowerManager.WakeLock mWakeLock;
  9.  
  10.         /** Called when the activity is first created. */
  11.         @Override
  12.         public void onCreate(final Bundle icicle) {
  13.                 setContentView(R.layout.main);
  14.        
  15.                 /* This code together with the one in onDestroy()
  16.                  * will make the screen be always on until this Activity gets destroyed. */
  17.                 final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
  18.                 this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
  19.                 this.mWakeLock.acquire();
  20.         }
  21.  
  22.         @Override
  23.         public void onDestroy() {
  24.                 this.mWakeLock.release();
  25.                 super.onDestroy();
  26.         }
  27. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


See: :src: http://code.google.com/android/referenc ... nager.html for further details.

Works with: SDK v1.0
Last edited by plusminus on Wed Oct 29, 2008 3:13 am, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Peli » Thu Oct 09, 2008 12:26 pm

wow, I would say this would use a lot of battery.

Users can not control when an application gets destroyed. This is handled by the system.

IMHO, it would be better to put the wakelock code in onResume() and onPause(). Then the phone stays awake while the application is in the foreground.

Peli
Peli
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Dec 27, 2007 10:54 am

Postby plusminus » Fri Oct 10, 2008 5:17 am

Yes it will (but user can still Dim the backlight).

I use it in AndNav and I have to, because it would suck if the screen would turn of during driving.

But thanks anyway, others can modify it your way.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Fri Oct 10, 2008 11:39 am

Still his point is valid: If you arrived and switch to a new app, your light will stay on. Maybe you could switch teh light setting when the app has focus/is on the foreground.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby plusminus » Fri Oct 10, 2008 2:18 pm

The user can only leave the Activity back to the menu, by going through a call of [font=Courier New]finish();[/font] (Also overrode BACK-Key.)

onDestroy() The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it),


Also I only want the SubActivities, like the SettingsMenu also to be enlightened.

So I still think I'm right :P
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Fri Oct 10, 2008 4:47 pm

But the home key still keeps working!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby spirolateral » Tue Oct 28, 2008 11:57 pm

any idea why SCREEN_BRIGHT_WAKE_LOCK wouldn't work? i obtain the lock in my onCreate and release it in onStop. i do it exactly as described in the docs but it's not making the screen bright.
spirolateral
Developer
Developer
 
Posts: 25
Joined: Tue Oct 14, 2008 5:15 am

Postby jasont » Wed Oct 29, 2008 1:07 am

Firstly I would not follow the code example in the original post. You need to control the power manager for the least amount of time possible.

You would be better off having a timer that "bumps" the screen every 20s. The minimum timeout on the G1 is 30s, so you might have to change this as more devices are released.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. if (lastWakeLock + DELAY <= currentTime) {
  3.  
  4.     mWakeLock.acquire();
  5.  
  6.     mWakeLock.release();
  7.  
  8.     lastWakeLock = currentTime;
  9.  
  10. }
  11.  
  12.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
jasont
Developer
Developer
 
Posts: 28
Joined: Mon Sep 22, 2008 6:39 pm
Location: California

Postby plusminus » Wed Oct 29, 2008 3:14 am

spirolateral wrote:any idea why SCREEN_BRIGHT_WAKE_LOCK wouldn't work? i obtain the lock in my onCreate and release it in onStop. i do it exactly as described in the docs but it's not making the screen bright.


Oh sorry I forgot to post that it requires a permission:

Required Permission:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.WAKE_LOCK" />
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ldrolez » Sun Apr 11, 2010 5:18 pm

I would suggest this to avoid a FC:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2.  
  3.        @Override
  4.  
  5.         public void onDestroy() {
  6.  
  7.                 if (mWakeLock.isHeld()) {
  8.  
  9.                         mWakeLock.release();
  10.  
  11.                 }
  12.  
  13.                 super.onDestroy();
  14.  
  15.         }
  16.  
  17.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
The Android Open Source ressources and software database.: http://www.aopensource.com/
http://twitter.com/ldrlz
ldrolez
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Mar 22, 2010 9:32 pm

Issue with PARTIAL_WAKE_LOCK

Postby Bhupathi » Mon Apr 19, 2010 1:17 pm

Hi plusminus,

My requirement is I want my activity running all the time even when the user presses the power button.
So I used PARTIAL_WAKE_LOCK in my code because "power manager" documentation says the CPU will be on even after the power button is pressed, which I think is not happening.

Is there any other method/process to keep my activity running always?

Regards,
Bhupathi
Bhupathi
Freshman
Freshman
 
Posts: 6
Joined: Tue Apr 13, 2010 10:23 am

back light not working

Postby murali.lancer » Mon Apr 26, 2010 1:00 pm

Hi plus-minus ,i used your code to control back light ,but this is not working .i used permissions also (<uses-permission android:name="android.permission.WAKE_LOCK" />).i am using sdk 1.5 .
and the code whatever u have posted ,that only i am using .is there anything to add ?
i am postin my source code just check once .

//check this code .
package com.murali;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.PowerManager;
import android.view.View;
import android.widget.Button;

public class BackLightControl extends Activity {
/** Called when the activity is first created. */
protected PowerManager.WakeLock mWakeLock;
private Button off;
private Button on;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
off=(Button)findViewById(R.id.btnBackLightOff);
on=(Button)findViewById(R.id.btnBackLightOn);
off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("back light off method");
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
mWakeLock.acquire();
}
});
on.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
System.out.println("on method");
mWakeLock.release();
}
});


}
@Override
public void onDestroy() {
System.out.println("on destroy method");
this.mWakeLock.release();
super.onDestroy();
}
//and manifest file is

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.murali"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BackLightControl"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>

//and layout file is


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:id="@+id/btnBackLightOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="off" />
<Button android:id="@+id/btnBackLightOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ON" />
</LinearLayout>



can u check this code and tell me whats wrong in this .I badly needed this implemtntation .


Thanks and regards
Murali Dhuli
murali.lancer
Developer
Developer
 
Posts: 37
Joined: Mon Jul 13, 2009 10:37 am
Location: banglore

Display of Key events

Postby V!j! Neelu » Thu Jul 29, 2010 6:27 pm

Hi murali Dhuli,
I need a code for the display of whatever virtual key event handled in an android mobile. I mean i want a playback kind of thing of all the virtual key events done.
V!j! Neelu
Freshman
Freshman
 
Posts: 5
Joined: Thu Jul 22, 2010 6:11 pm

Re: Screen Always On, PowerManager, WakeLock, Backlight

Postby duycm83 » Tue Aug 24, 2010 9:07 am

Giải pháp:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    public class HelloWorld extends Activity {
  2.       private static final int DELAY = 3000;  //3 seconds
  3.  
  4.      int defTimeOut = 0;
  5.    
  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState)
  8.     {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.hello_world);
  11.      
  12.        defTimeOut = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, DELAY);
  13.         Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, DELAY);
  14.     }
  15.  
  16.     @Override
  17.     protected void onDestroy() {
  18.         super.onDestroy();
  19.         Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, defTimeOut);
  20.     }
  21.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
duycm83
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Aug 24, 2010 8:58 am

Re: Screen Always On, PowerManager, WakeLock, Backlight

Postby @mir » Mon Aug 08, 2011 10:42 pm

Hi plusminus,

i am using wake lock with service, and in that service is am using ACCELEROMETER sensor but the problem is that when my phone screen is off ACCELEROMETER sensor also stop working and i dont understand why it is not working can u plz help me
<code>
@Override
public void onCreate() {
super.onCreate();
Log.e("Service", "Started ");
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();

pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK , getString(R.string.app_name) );

try {
wl.acquire();
Log.e("Service", "Started acquire ");
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sen = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(accelerationListener, sen, SensorManager.SENSOR_DELAY_GAME );
mPlayer = MediaPlayer.create(this, R.raw.nokia);

} catch (Exception e) {
Log.e( "Service" , "Unable to acquire wake lock" );
}
}

@Override
public void onDestroy() {

Log.e("Service", "Ended ");

if( wl != null) {
try {
wl.release();
mPlayer.stop();
sm.unregisterListener(accelerationListener);
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();

} catch (Exception e) {
Log.e( "Service" , "Unable to release wake lock" );
}
}

super.onDestroy();
}

private SensorEventListener accelerationListener = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
x = event.values[0];
y = event.values[1];
z = event.values[2];
refreshDisplay();

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
};



// this is own function
private void refreshDisplay() {

//if(flagVal == true && x > -3 && x < 3 && y > -3 && y < 3 && z < -7 && z > -13 ){
if( x > -3 && x < 3 && y > -3 && y < 3 && z < -7 && z > -13 ){
mPlayer.start();
}

}



</code>
@mir
Freshman
Freshman
 
Posts: 2
Joined: Sat Jul 23, 2011 5:08 pm

Top
Next

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests