package com.example.timing;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.PowerManager;import android.os.PowerManager.WakeLock;public class TimingActivity extends Activity { PowerManager manager = null; WakeLock lock = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_timing); manager = (PowerManager) this.getSystemService(Context.POWER_SERVICE); lock = manager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "mylock"); // PARTIAL_WAKE_LOCK :保持CPU 运转,屏幕和键盘灯有可能是关闭的。 // SCREEN_DIM_WAKE_LOCK :保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯 // SCREEN_BRIGHT_WAKE_LOCK :保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯 // FULL_WAKE_LOCK :保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度 } @Override protected void onPause() { super.onPause(); lock.release();// 关闭长亮 } @Override protected void onResume() { super.onResume(); lock.acquire();// 打开长亮 }}或者直接在onCreate()里,不用权限getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
版权声明:本文为博主原创文章,未经博主允许不得转载。