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");
    }
}



<service android:enabled="true" android:name=".MyServic"></service>

'IT 공부 > 안드로이드' 카테고리의 다른 글

Failed to install on device timeout ERROR  (2) 2012.10.27
Intent 활용 예시들  (0) 2012.10.26
안드로이드 한글 깨짐 현상 수정법  (2) 2012.03.10
android intent 여러값넘기기  (1) 2012.03.08
android animation 반복  (1) 2012.03.05

+ Recent posts