문제

I try to set the screen to always on however I cant figure out how to do it within a fragment. I have tried to get access to the ViewPager but it returns with a null value. I want the screen to be on for the whole application but it should be able to be changed by the user within the settings of the app.

Here is my code from within my fragment:

private void setIsAlwaysOn(boolean b)
{
    ViewPager pager = (ViewPager)getView().findViewById(R.id.pager);
    pager.setKeepScreenOn(b);

    sharePropertiesEditor.putBoolean(sp_alwaysOn, b);
    sharePropertiesEditor.commit();
}

I get a nullpointerexception at line 4 of the visible code above.

도움이 되었습니까?

해결책

Yeah. Can't you use this?

getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

For Kotlin, use:

activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top