2017-03-05 81 views
0

我添加到我的設置首選項,一個開關首選項,如果是的話,我想保持屏幕上,如果它是假回到默認設置的手機我用這個,但它不工作正確。保持屏幕上的開關首選項

if (sharedPreference.getBoolean("keep", true)) { 
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

     }else{ 

     getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    } 

回答

0
String TAG_WAKELOCK = "com.example.YourActivity.WAKE_LOCK_TAG"; 

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); 
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG); 

下的onPause();

wakelock.release(); 

under onResume();

wakelock.acquire(); 

確保您已在交換機喜好添加默認值

<SwitchPreference 
    android:key="keep" 
    android:title="@string/label" 
    android:defaultValue="true" />