일정시간 지난 후 자동로그아웃 기능을 구현중문의좀합니다(백그라운드)
안녕하세요 일정시간 지난 후 로그아웃 기능을 구현중입니다.
포어그라운드 에서는 10초뒤 로그아웃을 하는데..... 백그라운드로 가면 동작을 안하는것 같습니다.
뭘 더 추가해서 작업해야할지 감이 안잡히네요.
백그라운드로 이동시 onpause가 호출되므로 onpause메소드에 handler가 취소되어 백그라운드에서 로그아웃이
동작을 안하게 되는데 onpause를 사용 안하고 다른방법으로 백그라운드에서도 동작 할 수 있는 방법이 없을까요?
많은 의견 부탁드립니다 ㅠㅠ
-------------------------------------------------------------------------------------------------------------------------
Handler handler = new Handler();
Runnable autologout = new Runnable() {
@Override
public void run() {
// 10초뒤에 다음화면(MainActivity)으로 넘어가기 Handler 사용
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setClass(OrgMainListView.this, LogOnActivity.class);
startActivity(intent);
}
};
@Override
protected void onResume() {
super.onResume();
// 다시 화면에 들어어왔을 때 예약 걸어주기
handler.postDelayed(autologout, 10000); // 10초 뒤에 Runnable 객체 수행
}
@Override
protected void onPause() {
super.onPause();
// 화면을 벗어나면, handler 에 예약해놓은 작업을 취소하자
handler.removeCallbacks(autologout); // 예약 취소
}
onPause 직전에 발동되는 onUserLeaveHint 이벤트를 한번 사용해보시겠어요?
홈 버튼을 누를 때 onPause이벤트를 발동 전에 나오는 이벤트인데요 이때 시간을 등록해두고
사용자가 다시 돌아왔을 때 현재 시간과 비교해서 로그아웃 시켜주는 방법도 괜찮을것같습니다.
onUserLeaveHint의 설명으로는
Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.
This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.
활동이 사용자 선택의 결과로 백그라운드로 들어갈 예정인 경우 활동 라이프 사이클의 일부로 호출됩니다. 예를 들어, 사용자가 Home 키를 누르면 onUserLeaveHint ()가 호출되지만 들어오는 호출로 인해 호출 작업이 자동으로 포어 그라운드로 이동하면 onUserLeaveHint ()가 중단되는 작업에 대해 호출되지 않습니다 . 호출 될 경우이 메서드는 활동의 onPause () 콜백 직전에 호출됩니다.
이 콜백 및 onUserInteraction ()은 상태 표시 줄 알림을 지능적으로 관리하는 활동을 돕기위한 것입니다. 구체적으로, 활동이 통지를 취소하기위한 적절한 시간을 결정하도록 돕는다.
-----------------------
단순히 다시 접속했을 때 로그아웃 상태를 원하는 것이 아닌 백그라운드에서 로그아웃을 무조건 진행해야 한다고 하면
아래 글을 읽어보시고 워커매니저를 사용해보시는건 어떨까요
화면의 생명주기로 모든것을 커버할 수 없습니다.
앱이 강제종료 되었을때 onPause 같은 생명주기에 관련 된 메소드는 호출 되지 않습니다.
보다 정확하게 기능을 구현하고자 하시면, 안드로이드의 '서비스'를 이용해보시는게 좋을듯 합니다.
서비스는 activity, fragment 같은 화면과 별개로 단독적으로 구현됩니다.
서비스를 활용하는 앱은 음악재생앱이 대표적일것 같습니다.
아래는 안드로이드 서비스에 대해 소개 해주는 글 입니다.
https://developer.android.com/guide/components/services?hl=ko