Duc*_*ran 219
在onCreate()中的setContentView()之后添加一行代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Run Code Online (Sandbox Code Playgroud)
Tar*_*ngh 118
这里已经有很多答案了!我正在通过其他可靠的解决方案回答这个问题:
使用PowerManager.WakeLock
不是一个可靠的解决方案,因为应用程序需要额外的权限.
<uses-permission android:name="android.permission.WAKE_LOCK" />
Run Code Online (Sandbox Code Playgroud)
此外,如果它意外地保持唤醒锁定,它可以使屏幕保持打开状态.
所以,我建议不要使用该PowerManager.WakeLock
解决方案.而不是这个,使用以下任何解决方案:
第一:
我们可以用getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
在onCreate()
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Run Code Online (Sandbox Code Playgroud)
第二:
我们可以用 keepScreenOn
1.setKeepScreenOn()
在java代码中使用实现:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View v = getLayoutInflater().inflate(R.layout.driver_home, null);// or any View (incase generated programmatically )
v.setKeepScreenOn(true);
setContentView(v);
}
Run Code Online (Sandbox Code Playgroud)
文档http://developer.android.com/reference/android/view/View.html#setKeepScreenOn(boolean)
2.添加keepScreenOn
到xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true" >
Run Code Online (Sandbox Code Playgroud)
文档http://developer.android.com/reference/android/view/View.html#attr_android%3akeepScreenOn
笔记(一些有用的点):
keepScreenOn
应该在Main/Root/Parent View上使用它并不重要.它可以与任何子视图一起使用,并且工作方式与它在父视图中的工作方式相同.Din*_*rma 101
使用PowerManager.WakeLock类来执行此操作.请参阅以下代码:
import android.os.PowerManager;
public class MyActivity extends Activity {
protected PowerManager.WakeLock mWakeLock;
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle icicle) {
setContentView(R.layout.main);
/* This code together with the one in onDestroy()
* will make the screen be always on until this Activity gets destroyed. */
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
}
@Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}
Run Code Online (Sandbox Code Playgroud)
使用清单文件中的以下权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Run Code Online (Sandbox Code Playgroud)
希望这能解决你的问题... :)
Xar*_*mer 49
不要使用唤醒锁.
It requires permission and other stuff and may cause error if you forget to set it in right time.
Run Code Online (Sandbox Code Playgroud)
最简单的方法是在您想要保持屏幕开启时使用以下代码.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Run Code Online (Sandbox Code Playgroud)
如果要删除或终止keep_Screen_on,则为答案添加一个
getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Run Code Online (Sandbox Code Playgroud)
你也可以看到这里 ..
并且the best and easiest way
.. android:keepScreenOn="true"
在您的活动的布局根目录中使用 相同的东西没有额外的代码.但它仍将保留在Keep_Scree_on状态..
可根据您的需求而变化请参见此处
Pra*_*ani 15
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Run Code Online (Sandbox Code Playgroud)
getWindow
是为活动定义的方法,不需要您找到View
第一个.
小智 10
有多种方法可以做到:
解决方案1:
class MainActivity extends AppCompactActivity {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案2:
在 activity_main.xml 文件中,只需添加:
<android:KeepScreenOn="true"/>
Run Code Online (Sandbox Code Playgroud)
我的建议:请不要使用 WakeLock。如果你使用它,你必须定义额外的权限,而且这个东西主要是在 CPU 的开发环境中很有用。
另外,请确保在关闭活动时关闭屏幕。你可以这样做:
public void onDestry() {
getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Run Code Online (Sandbox Code Playgroud)
添加 android:keepScreenOn="true"
要保持屏幕的活动的XML是最佳选择.将该行添加到活动的主布局中.
像这样的东西
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
128546 次 |
最近记录: |