首先添加权限:
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
实现代码:
1: public class test extends Activity
2: {
3: PowerManager powerManager = null;
4: WakeLock wakeLock = null;
5: @Override
6: protected void onCreate(Bundle savedInstanceState) {
7: super.onCreate(savedInstanceState);
8: this.setContentView(R.layout.main);
9:
10: this.powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
11: this.wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
12: }
13: @Override
14: protected void onResume() {
15: super.onResume();
16: this.wakeLock.acquire();
17: }
18: @Override
19: protected void onPause() {
20: super.onPause();
21: this.wakeLock.release();
22: }
23: }
说明:在不同的生命周期调用不用的WakeLock函数可以使系统正常运行(如果不调用WakeLock.release,则屏幕会一直常亮)。