IT story

Android 기기가 프로그래밍 방식으로 잠자 지 않게하려면 어떻게해야합니까?

hot-time 2020. 6. 16. 08:06
반응형

Android 기기가 프로그래밍 방식으로 잠자 지 않게하려면 어떻게해야합니까?


Android 기기가 프로그래밍 방식으로 잠자 지 않게하려면 어떻게해야합니까?


한 가지 옵션은 웨이크 잠금 을 사용하는 것 입니다. 문서의 예 :

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();

// screen and CPU will stay awake during this section

wl.release();

의 테이블도있다 이 페이지 wakelocks의 다른 종류를 설명합니다.

깨우기 잠금을 사용할 때는 약간의주의가 필요합니다. release()잠금을 완료 한 경우 (또는 포 그라운드가 아닌 경우) 항상 잠금 상태 를 유지 하십시오. 그렇지 않으면 앱에서 배터리가 많이 소모되고 CPU 사용량이 발생할 수 있습니다.

이 설명서에는 장치를 깨우는 방법과 사용 방법을 설명하는 다양한 방법을 설명 하는 유용한 페이지 도 포함되어 있습니다 . "장치가 절전 모드로 전환되지 않도록 방지"가 화면 만 참조 하고 CPU를 활성 상태로 유지하지 않으면 깨우기 잠금이 필요한 것 이상일 수 있습니다.

이 방법을 사용하려면 매니페스트에 WAKE_LOCK 권한이 설정되어 있어야합니다 .


당신은 단지 특정의 절전 모드를 방지하려면 View, 단지 전화 setKeepScreenOn(true)것이에 View설정된 나 keepScreenOn에 속성을 true. 이렇게하면가 화면 View에있는 동안 화면이 꺼지지 않습니다 . 이에 대한 특별한 권한이 필요하지 않습니다.


다른 작동 솔루션을 찾았습니다 : onCreate 이벤트 아래 앱에 다음 줄을 추가하십시오.

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

샘플 Cordova 프로젝트는 다음과 같습니다.

package com.apps.demo;
import android.os.Bundle;
import android.view.WindowManager;
import org.apache.cordova.*;

public class ScanManActivity extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.loadUrl("http://stackoverflow.com");
    }
}

그 후에 내 앱이 열려있는 동안 잠자기 상태가되지 않았습니다. ans가 xSus에 간다.


android:keepScreenOn="true" 레이아웃 XML에서 얻는 것이 더 나은 옵션 일 수 있습니다.

추가 정보 : https://developer.android.com/training/scheduling/wakelock.html


아래와 같이 활동 창에 플래그 설정

@Override public void onResume() {
 super.onResume();
 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

@Override public void onPause() {
 super.onPause();
 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

루트 쉘 (예 : adb 쉘)에서 다음을 사용하여 잠글 수 있습니다.

echo mylockname >/sys/power/wake_lock    

그 후에는 장치가 깨어있을 때까지 다음을 수행합니다.

echo mylockname >/sys/power/wake_unlock    

'mylockname'과 동일한 문자열입니다.

이렇게해도 화면이 어두워지는 것은 아니지만 CPU가 절전 모드가되지는 않습니다.

/ sys / power / wake_lock은 사용자 라디오 (1001)와 그룹 시스템 (1000) 그리고 물론 루트에 대한 읽기-쓰기입니다.

A reference is here: http://lwn.net/Articles/479841/


what @eldarerathis said is correct in all aspects, the wake lock is the right way of keeping the device from going to sleep.

I don't know waht you app needs to do but it is really important that you think on how architect your app so that you don't force the phone to stay awake for more that you need, or the battery life will suffer enormously.

I would point you to this really good example on how to use AlarmManager to fire events and wake up the phone and (your app) to perform what you need to do and then go to sleep again: Alarm Manager (source: commonsware.com)


If you are a Xamarin user, this is the solution:

   protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle); //always call superclass first

        this.Window.AddFlags(WindowManagerFlags.KeepScreenOn);

        LoadApplication(new App());
    }

참고URL : https://stackoverflow.com/questions/3723634/how-do-i-prevent-an-android-device-from-going-to-sleep-programmatically

반응형