第一步: 首先添加权限:
- <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
第二步:代码实现如下:
- public class ScreenActivity extends Activity
- {
- PowerManager powerManager = null;
- WakeLock wakeLock = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.main);
- powerManager = (PowerManager)this.getSystemService(this.POWER_SERVICE);
- wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
- }
- @Override
- protected void onResume() {
- super.onResume();
- wakeLock.acquire();
- }
- @Override
- protected void onPause() {
- super.onPause();
- wakeLock.release();
- }
- }