1.
为窗口添加flag:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
或者
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2.
设置Layout中View的keepScreenOn属性: android:keepScreenOn="true"
必须是可显示view才会起作用。
3.
使用电源管理:
public class WakeLockActivity 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(); } }
添加权限:
<uses-permission android:name="android.permission.WAKE_LOCK"> </uses-permission>