저는 제작하려고하는 Android 코드와 관련하여 약간의 문제가 있습니다. 내가하려는 일은 사용자가 전화기를 잠금 해제하고 프로그램을 처음 시작할 때 1을 표시하고 사용자가 화면 잠금을 눌러 전화기를 잠그면 0을 단순히 인쇄하는 것입니다. 나는 이것이 onPause와 onResume을 사용해 보았으므로 안드로이드 라이프 사이클과 관련이 있다고 생각했지만, 내 프로그램은 0, 결코 1을 출력하지 않았다. logTime 메서드는 단순히 1 또는 0을 출력하고 MainActivity의 onCreate 메서드는 onResume() 메서드를 한 번 호출합니다. 여기 내 코드입니다 :Android에서 화면을 켜고 끕니다.
MainActivity :
protected void onPause(){
if(ScreenReceiver.screenOn){
logTime(ScreenReceiver.screenOn);
super.onPause();
}
protected void onResume()
if(!ScreenReceiver.screenOn){
logTime(!ScreenReceiver.screenOn);
super.onResume();
}
화면 수신기 : 그것은 단지 0의 출력합니다 왜
public class ScreenReceiver extends BroadcastReceiver{
public static boolean screenOn;
@Override
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
screenOn = false;
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
screenOn = true;
}
}
}
는 잘 모르겠어요. 이유를 아는 사람이 있습니까? 감사!
'logTime'해야합니다 단지'logTime (ScreenReceiver.screenOn),'더'' –
@ A - C 흠 괜찮 (ScreenReceiver.screenOn!가!).하지 지금 문제가 있습니다. 응용 프로그램을 다시 시작할 때마다 1을 기록 할 수 없습니다. 화면 잠금을 해제해도 아무 것도 인쇄되지 않지만 화면을 잠그면 0이 인쇄됩니다. 왜? – user200081
수신자를 등록/등록 취소 하시겠습니까? 당신이 가지고있는 것은 (기본적으로 같은 매개 변수로'logTime()'을 호출하는 것과는 별도로) 올바르게 보인다. –