1.可以在AndroidManifest.xml中静态注册:
<uses-permission android:name="android.permission.WAKE_LOCK" />
2.通过代码动态注册:
方法一:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
方法二:
在布局文件中添加android:keepScreenOn=”true”
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
这两种方法可以在你想要的任何地方进行修改 getWindow() .clearFlags(WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON