본문 바로가기

개발 이야기

Service 에 대한 이해.

320x100

서비스에 대한 이해가 필요하다.

남이 짜 놓은 소스에서 아래의 오류가 발생해서 확인하니 내가 서비스에 대해 너무 몰라서 생긴 문제였다.


간단하게 잘 정리한 블로그가 있어 출처를 남기고 퍼 옵니다. (소실방지)

http://woosa7.tistory.com/193




Service는 background에서 처리를 계속할 수 있는 클래스이다.
Service는 기본적으로 activity를 가지지 않는다.

서비스를 구현하기 위한 3가지 절차
-- Service 클래스를 확장한 새로운 클래스 정의
-- Manifest file에 Service 선언 추가
-- App에서 Service 실행


Service는 background에서 처리를 계속할 수 있는 클래스이다.
Service는 기본적으로 activity를 가지지 않는다.

서비스를 구현하기 위한 3가지 절차
-- Service 클래스를 확장한 새로운 클래스 정의
-- Manifest file에 Service 선언 추가
-- App에서 Service 실행



1. 서비스를 실행하는 클래스 - 타이머를 이용한 반복 처리.


public class MyService extends Service implements Runnable {

    // 시작 ID
    private int mStartId;
    // 서비스에 대한 스레드에 연결된 Handler. 타이머 이용한 반복 처리시 사용.
    private Handler mHandler;
    // 서비스 동작여부 flag
    private boolean mRunning;
    // 타이머 설정 (2초)
    private static final int TIMER_PERIOD = 2 * 1000; 
    private static final int COUNT = 10;
    private int mCounter;


    // 서비스를 생성할 때 호출
    public void onCreate() {
        Log.e("MyService", "Service Created.");
        super.onCreate();
        mHandler = new Handler();
        mRunning = false;
    }


    // 서비스 시작할 때 호출. background에서의 처리가 시작됨.
    // startId : 서비스 시작요구 id. stopSelf에서 종료할 때 사용. 

     //onStart는 여러번 호출될 수 있기 때문에 식별자로 사용.

    public void onStart(Intent intent, int startId) {
        Log.e("MyService", "Service startId = " + startId);
        super.onStart(intent, startId);
        mStartId = startId;
        mCounter = COUNT;

        // 동작중이 아니면 run 메소드를 일정 시간 후에 시작
        if (!mRunning) {
              // this : 서비스 처리의 본체인 run 메소드. Runnable 인터페이스를 구현 필요.
              // postDelayed : 일정시간마다 메소드 호출
              mHandler.postDelayed(this, TIMER_PERIOD);
              mRunning = true;
        }
    }

 

    // 서비스의 종료시 호출
    public void onDestroy() {
        // onDestroy가 호출되어 서비스가 종료되어도 
        // postDelayed는 바로 정지되지 않고 다음 번 run 메소드를 호출.
        mRunning = false;
        super.onDestroy();
    }


    // 서비스 처리
    public void run() {
        if (!mRunning) {
            // 서비스 종료 요청이 들어온 경우 그냥 종료
            Log.e("MyService", "run after destory");
            return;
        } else if (--mCounter <= 0) {
            // 지정한 횟수 실행하면 스스로 종료
            Log.e("MyService", "stop Service id = "+mStartId);
            stopSelf(mStartId);
        } else {
            // 다음 작업을 다시 요구
            Log.e("MyService", "mCounter : " + mCounter);
            mHandler.postDelayed(this, TIMER_PERIOD);
        }
    }

    // 원격 메소드 호출을 위해 사용
    // 메서드 호출을 제공하지 않으면 null을 반환
    public IBinder onBind(Intent intent) {
        return null;
    }
}




2. 서비스 실행, 종료를 사용자가 요청하는 클래스


// 서비스 시작과 종료를 요구하는 Activity
public class MyServiceActivity extends Activity {

    ComponentName mService;    // 시작 서비스의 이름
    TextView mTextView;              // 서비스 상태 표시


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mTextView = (TextView)findViewById(R.id.text_view);
        
        Button start = (Button)findViewById(R.id.start_button);
        start.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                startHelloService();
            }});
        
        Button stop = (Button)findViewById(R.id.stop_button);
        stop.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                stopHelloService();
            }});
    }


    // 서비스 시작 요청
    private void startHelloService() {
        mService = startService(new Intent(this, MyService.class));
        mTextView.append(mService.toShortString()+" started.\n");
    }

 

    // 실행한 서비스 중지 요청

    private void stopHelloService() {
        if (mService == null) {
            mTextView.append("No requested service.\n");
            return;
        }
        
        Intent i = new Intent();
        i.setComponent(mService);
        if (stopService(i))
            mTextView.append(mService.toShortString()+" is stopped.\n");
        else
            mTextView.append(mService.toShortString()+" is alrady stopped.\n");
    }
}



3. 사용자가 서비스 실행, 종료하는 화면 구성


<!-- 시작 -->
<Button android:id="@+id/start_button" android:text="Start"
    android:layout_width="fill_parent" android:layout_height="wrap_content" />

<!-- 종료 -->
<Button android:id="@+id/stop_button" android:text="Stop"
    android:layout_width="fill_parent" android:layout_height="wrap_content" />

<!-- 상태표시 -->
<TextView  android:id="@+id/text_view_id"

    android:editable="true"
    android:layout_width="fill_parent" android:layout_height="fill_parent" />

반응형