在不同的生命周期调用不用的WakeLock函数,可以使系统正常运行(如果不调用WakeLock.release,则屏幕会一直常亮)。
添加权限。
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>实现代码。
public class test extends Activity { PowerManager powerManager = null; WakeLock wakeLock = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); this.powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE); this.wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock"); } @Override protected void onResume() { super.onResume(); this.wakeLock.acquire(); } @Override protected void onPause() { super.onPause(); this.wakeLock.release(); } }