Android保持屏幕長亮的解決方案

總結一下總共有3種方案

方案1:

1、在AndroidManifest.xml中添加權限

<!-- 屏幕長亮 -->
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

2、添加代碼

PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");

在onResume()中添加

wakeLock.acquire();

在onPause()中添加

wakeLock.release(); 

 方案2:

在需要長亮的activityonCreate()內加上一句話 

//保持屏幕長亮
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

方案3

在需要長亮的activity對應的xml佈局文件內加上一句話android:keepScreenOn="true"

如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:keepScreenOn="true">
</RelativeLayout>


 


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章