You surely want to keep the screen of your phone ON and you don’t know how to do this, otherwise you wouldn’t be on our blog :P But, you are on the right place, because in this tutorial I will show you how to keep the screen on programatically.
1. Create a new project and call your java class(the one that is generated by Eclipse or other IDE)“MyActivity”.
2. Go to MyActivity class and put the following code:
package com.example; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.PowerManager; public class MyActivity extends Activity { private PowerManager.WakeLock mWakeLock; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // this is how you can keep the screen ON and don't let the phone to lock final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Tag"); this.mWakeLock.acquire(); } @Override public void onDestroy() { this.mWakeLock.release(); super.onDestroy(); } }
3. And the last step, put the following permission into the AndroidManifest.xml file:
<uses -permission="-permission" android:name="android.permission.WAKE_LOCK">
So the AndroidManifest.xml file will look like this: