앱의 휴지 시간

조회수 1731회

제가 만들고 있는 앱에 A->B->C->A로 구성된 세가지 액티비티가 존재합니다. 현재 액티비티와 상관없이 15분동안 앱이 휴지 상태에 있으면 이를 감지하고 메시지를 보이고자 합니다. 이를 구현할 수 있는 가장 좋은 방법은 무엇일까요?

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    저라면 이런 방법을 사용할 것 같습니다 :

    1. 앱의 휴지 시간을 관리할 쓰레드를 만든다.
    2. 이 쓰레드를 앱에서 실행시킨다.
    3. 사용자의 상호 작용이 있을 때마다 휴지 시간을 갱신한다.

    휴지 시간을 관리하는 전역 쓰레드를 저장하는 클래스

    public class ControlApplication extends Application
    {
        private static final String TAG=ControlApplication.class.getName();
        private Waiter waiter;  //휴지 시간을 컨트롤하는 쓰레드
    
        // 급하지 않은 초기화만 이곳에서 이루어집니다!
        @Override
        public void onCreate()
        {
            super.onCreate();
            Log.d(TAG, "Starting application"+this.toString());
            waiter=new Waiter(15*60*1000); //15 mins
            waiter.start();
        }
    
        public void touch()
        {
            waiter.touch();
        }
    }
    

    모든 액티비티의 부모로 사용될 클래스

    public class ControlActivity extends Activity
    {
        private static final String TAG=ControlActivity.class.getName();
    
        /**
         * Gets reference to global Application
         * @return must always be type of ControlApplication! See AndroidManifest.xml
         */
        public ControlApplication getApp()
        {
            return (ControlApplication )this.getApplication();
        }
    
        @Override
        public void onUserInteraction()
        {
            super.onUserInteraction();
            getApp().touch();
            Log.d(TAG, "User interaction to "+this.toString());
        }
    
    }
    

    마지막으로 휴지 시간을 관리하는 쓰레드입니다.

    public class Waiter extends Thread
    {
        private static final String TAG=Waiter.class.getName();
        private long lastUsed;
        private long period;
        private boolean stop;
    
        public Waiter(long period)
        {
            this.period=period;
            stop=false;
        }
    
        public void run()
        {
            long idle=0;
            this.touch();
            do
            {
                idle=System.currentTimeMillis()-lastUsed;
                Log.d(TAG, "Application is idle for "+idle +" ms");
                try
                {
                    Thread.sleep(5000); //매 5초마다 확인
                }
                catch (InterruptedException e)
                {
                    Log.d(TAG, "Waiter interrupted!");
                }
                if(idle > period)
                {
                    idle=0;
                    //팝업을 호출하는 등의 코드를 여기에 삽입하세요.
                }
            }
            while(!stop);
            Log.d(TAG, "Finishing Waiter thread");
        }
    
        public synchronized void touch()
        {
            lastUsed=System.currentTimeMillis();
        }
    
        public synchronized void forceInterrupt()
        {
            this.interrupt();
        }
    
        //쓰레드를 중지
        public synchronized void stop()
        {
            stop=true;
        }
    
        public synchronized void setPeriod(long period)
        {
            this.period=period;
        }
    
    }
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)