Turn screen ON OFF Android sample code

WakeLock, PowerManager,uses-permission Android sample.
Main.java

public class Main extends Activity {
	    private SensorManager mSensorManager;
	    private PowerManager mPowerManager;
	    private WindowManager mWindowManager;
	    private WakeLock mWakeLock;
	    private Button button;
	    private TextView textView;
 
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
try{
	        // Get an instance of the SensorManager
	        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
 
	        // Get an instance of the PowerManager
	        mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
 
	        // Get an instance of the WindowManager
	        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
	        mWindowManager.getDefaultDisplay();
 
	        // Create a bright wake lock
	        mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass()
	                .getName());
 
	        setContentView(R.layout.main);
	        textView = (TextView)findViewById(R.id.textView1);
	        button = (Button)findViewById(R.id.button1);
	        button.setOnClickListener(mButtonStopListener);
 
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("onCreate", e.getMessage());
		}
} // END onCreate
 
	    View.OnClickListener mButtonStopListener = new OnClickListener() {
	    	public void onClick(View v) {
		        try {
					mWakeLock.release();
					textView.setText("mWakeLock.release()");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Log.e("onPause",e.getMessage());
				}
 
	    	}
	    };
 
	    @Override
	    protected void onResume() {
	        super.onResume();
	        /*
	         * when the activity is resumed, we acquire a wake-lock so that the
	         * screen stays on, since the user will likely not be fiddling with the
	         * screen or buttons.
	         */
 
	        try {
				mWakeLock.acquire();
				textView.setText("mWakeLock.acquire()");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("onResume", e.getMessage());
			}
 
	    }
 
	    @Override
	    protected void onPause() {
	        super.onPause();
 
	        // and release our wake-lock
	        try {
				mWakeLock.release();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("onPause",e.getMessage());
			}
	    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cz.okhelp.Main"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.HARDWARE_TEST"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
 
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
 
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ScreenBrightnessActivity"
                  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>
</manifest>
This entry was posted in Android Examples Code and tagged , , , . Bookmark the permalink.


Comments are closed.

57,725 views