본문 바로가기

Daily Sample

[Day22] Service와 Alarm으로 실행되는 Androday FakeCall App 소스

TITLE : Androday FakeCall App 

Date : 2010/08/31

Description : 

This is Source that Androday FakeCallApp by excuting Service & Alarm

Download Link :  

Reference Site : http://www.androidpub.com/278799#comment_279291 , http://android-town.org , Android API

 이전 시간에는 안드로이드가 아이폰과 비교해서 가지는 장점으로 백그라운드 서비스라고 언급했었다.

그렇다면 백그라운드 서비스가  항상 좋은것이냐고 물어본다면 대답은 No 라고 얘기하고 싶다.

시스템성능과 하드웨어 제약사항을 고려하지않고 백그라운드 서비스를 한다면 최악이 App이 될수 있기 때문이다.

(실제로 Windows Moblie 에서는 Form에서 백그라운드를 기본지원하여 사용자들의 UX를 저하시켰다;)

결론적으로 ,  백그라운드 서비스가 적재적소에 사용된다면 Best App을 만들어낼 수 있다고 본다. 

오늘은 Service와 Alarm Manager 를 이용해서 Fake Call을하는 App을 만들어 본다.   

Alarm Manager 에 대한 자세한 설명 링크 : http://www.androidpub.com/102370

1. 시작버튼을 누르게되면 Alarm Manager는 5초를 기다린 후 Sevice를 상속받는 FakeCallSevice 를 실행시킨다. 

또, Notification Manager를 이용해 알람이 작동됨을 화면 상단에  표시한다.

 Date t = new Date();

 t.setTime(java.lang.System.currentTimeMillis() + 5*1000);

AM.set(AlarmManager.RTC_WAKEUP, t.getTime(), ServicePending);

//RTC_WAKEUP 플래그는 대기모드 상태에서도 알람이 작동함을 의미함.

 


2. 구지 App이 실행되고 있다는걸 Notification으로 알려주는 이유는 서비스로 실행될 예정이기 때문이다.

사용자는 다른 화면에서도 Notification을 클릭해서 App을 종료해 줄 수 있다.

소스로 살펴보면 다시 돌아올 PendingIntent로 지정하는 부분이 있다.

Notification notification = new Notification(R.drawable.stat_sample, “FakeCall 시작”,
                  System.currentTimeMillis());    

notification.setLatestEventInfo(getApplicationContext(), “Fake”, “Fake Call App”, NotificationPending);                  
        
NM.notify(notificationid, notification);       


3.  Call me를 클릭하면 5초후 알람이 작동하고, 서비스가 시작하게 되는데

실제로는 FakeCallService가 작동하고 보여지는 화면은 FakeCallActivity에서 구성된다.

특히, Activity에서는 대기모드에서 화면이 켜질수 있도록 플래그 설정을 해줘야한다.

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);



4. FakeCallService가 시작하면 onCreate()에서 FakeCallActivity를 뛰우고,

OnStart() 부분에서 Thread가 시작되면서 진동 무한루프가 돌게된다.

(서비스가 종료될때 무한루프는 종료된다.)
 

<서비스 onCreate()> 

//서비스에서 Activity를 실행시키는 방법

Intent intent = new Intent(getBaseContext(), FakeCallActivity.class);
PendingIntent pi = PendingIntent.getActivity( getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
pi.send();

<서비스 onCreate()> 

//서비스에서 스레드 도는 부분 2초에 한번씩 1초짜리 진동이 울린다.

 while(isRunning)
    { 
     try {            
      Log.e(“Androday”, “Ring ~ “);       
            vibrator.vibrate(1000);

           Thread.sleep(2000);          
     } 


5. Stop 버튼을 누르게되면 , 알람/서비스/알림창을 취소한다.

              stopService(intent);
             //서비스 취소
             AM.cancel(ServicePending);    
              //알람 취소            
             NM.cancel(notificationid);
             //알림창 취소 

 

<P.S> 이전 서비스 App과는 다르게 스마트폰이 대기모드로 들어가더라도 Alarm으로 깨워서 서비스를 실행 시킨다.

주기적인 체크가 필요한 App일경우 Alarm을 이용한 방법도 적합해 보인다.  

Creative Commons License
이 저작물은 크리에이티브 커먼즈 저작자표시 3.0 Unported 라이선스에 따라 이용할 수 있습니다.