[Android]APP中保持屏幕不自动灭屏的一种方法

在 app 的 manifest.xml 中定義 permission, 避免报错,

<uses-permission android:name="android.permission.WAKE_LOCK"/>

然後在onCreate的 setContentView(R.layout.activity_main)之前添加以下code,

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
重新編譯安裝apk,屏幕就不會再自动灭屏了。

猜你喜欢

转载自blog.csdn.net/champwang/article/details/80600424