可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using the method setKeepScreenOn(true) and haven't been able to figure out how to call this in relation to the current Activity (which has a content view set). I've been able to get it to work by calling it on one of my buttons which is always present in the view, but this feels wrong - and I'm sure there must be a way to get around this. I tried referencing the current focus like this:
getCurrentFocus().setKeepScreenOn(true);
but that threw a NullPointerException. Maybe there was no current focus. So, can anyone tell me how I can reference the view class which I am working inside? Thanks :)
回答1:
Try this answer:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow
is a method defined for activities, and won't require you to find a View
first.
回答2:
As Hawk said but poorly explained.
You can also use FLAG_KEEP_SCREEN_ON
in your XML layout file.
Note the android:keepScreenOn="true"
I've now written all the choices for keeping the screen on up into a blog post:
http://blog.blundellapps.com/tut-keep-screen-onawake-3-possible-ways/
回答3:
Set android:keepScreenOn
in XML
回答4:
If you are doing it on a class extends View. You can simple:
this.setKeepScreenOn(true);
回答5:
According to Google Docs for android Developers you've two ways to do this :
First way :
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); }
Second way is to add in your xml file layout this attribute: android:keepScreenOn="true"