앱에서 화면을 유지하려면 어떻게합니까? [복제]
이 질문에는 이미 답변이 있습니다.
- 답변 3 개 강제 실행
내 안드로이드 앱의 경우 전화가 잠기거나 백라이트가 꺼지는 것을 원하지 않습니다.
이를 수행 하려면 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();
}
}
매니페스트 파일에서 다음 권한을 사용하십시오.
<uses-permission android:name="android.permission.WAKE_LOCK" />
이것이 당신의 문제를 해결할 것입니다 ... :)
onCreate ()에서 setContentView () 다음에 한 줄의 코드 추가
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
많은 답변이 이미 존재합니다! 추가적이고 신뢰할 수있는 솔루션 으로이 질문에 대답하고 있습니다.
PowerManager.WakeLock
앱에 추가 권한이 필요하므로 솔루션을 사용하는 것이 그렇게 신뢰할 수 없습니다.
<uses-permission android:name="android.permission.WAKE_LOCK" />
또한 실수로 깨우기 잠금을 유지 한 경우 화면을 그대로 둘 수 있습니다.
따라서 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);
}
둘째:
우리는 사용할 수 있습니다 keepScreenOn
1.setKeepScreenOn()
자바 코드를 이용한 구현 :
@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);
}
문서 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" >
문서 http://developer.android.com/reference/android/view/View.html#attr_android%3akeepScreenOn
참고 사항 (유용한 사항) :
keepScreenOn
Main / Root / Parent View에서 사용해야 하는 것은 중요하지 않습니다 . 모든 하위 뷰에서 사용할 수 있으며 상위 뷰에서 작동하는 것과 같은 방식으로 작동합니다.- 중요한 것은 뷰의 가시성을 볼 수 있어야한다는 것입니다. 그렇지 않으면 작동하지 않습니다!
Don't Use Wake Lock.
It requires permission and other stuff and may cause error if you forget to set it in right time.
The easiest way is to use the below code when you want to keep your screen on..
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
One addition to the answer if you want to remove or terminate keep_Screen_on
getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
you can also see here..
And the best and easiest way
.. Using android:keepScreenOn="true"
in layout root of your activity does the same thing without extra code. But it will remain it in Keep_Scree_on State..
It can be vary on your demand See here
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.
You can simply use setKeepScreenOn()
from the View class.
Adding android:keepScreenOn="true"
in the XML of the activity(s) you want to keep the screen on is the best option. Add that line to the main layout of the activity(s).
Something like this
<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>
You need to use Power Manager to acquire a wake lock in your application.
Most probably you are interested in a FULL_WAKE_LOCK:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
....
wl.release();
There are multiple ways you can do it:
Solution 1:
class MainActivity extends AppCompactActivity {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
Solution 2:
In activity_main.xml file, simply add:
<android:KeepScreenOn="true"/>
My advice: please don't use WakeLock. If you use it, you have to define extra permission, and mostly this thing is useful in CPU's development environment.
Also, make sure to turn off the screen while closing the activity. You can do it in this way:
public void onDestry() {
getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
At this point method
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
is deprecated.
You should use getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
and getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
No need to add permission and do tricks. Just use below text in your main layout.
android:keepScreenOn="true"
참고URL : https://stackoverflow.com/questions/5712849/how-do-i-keep-the-screen-on-in-my-app
'programing tip' 카테고리의 다른 글
Brew 설치 도커에 도커 엔진이 포함되어 있지 않습니까? (0) | 2020.07.22 |
---|---|
RHEL에 Python 3 설치 (0) | 2020.07.22 |
JavaScript를 사용하여 HTML에서 textArea에 maxlength를 적용하는 방법 (0) | 2020.07.21 |
플래그 열거 형이 일반적으로 16 진 값으로 정의되는 이유 (0) | 2020.07.21 |
이름 노드가 안전 모드에 있습니다. (0) | 2020.07.21 |