woong's

Android Service 사용 하기 본문

Develop/Android

Android Service 사용 하기

dlsdnd345 2016. 2. 14. 12:52
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Android Service 사용 하기


​안녕하세요 . Android 를 하면 간혹 Android Service 를 이용하곤 합니다 . 

앱이 죽었을경우에 어떤 행동을 하기 위해서 Service를 사용하곤 하죠 .


예를 들어 음악을 플레이 한다던가 , 푸시 , SMS 를 받는다던가 여러군데에서 이용을 하고 있습니다 .


이렇게 많은 곳에서 사용 하고 있는데 가끔 사용한다고 정확히 알고 있지 않아 정작 사용하려면

헤매는 그런 일이 생겨서 ^^;;; 이렇게 블로그를 포스트를 통해서 정리해보려 합니다 .



Service 사용 준비


1. Service 만들기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.example.service;
 
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
 
public class TestService extends Service {
 
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        
        System.out.println("서비스 Start");
        
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        
        System.out.println("서비스 Finish");
    }
 
}
 

 

​서비스는 간단하게 시작과 종료를 만들어 보았습니다 .

위코드를 보면 정말 간단하고 명료 합니다 .

 

 

2. Manifast.xml 에 Service 등록


위 서비스를 사용하기 위해서는 Manifast에 Service 를 등록 해야 합니다 .

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.servicetest"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <service android:name="com.example.service.TestService" >
            <intent-filter>
                <action android:name="com.handstudio.android.services" />
 
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
 
        <activity
            android:name="com.example.servicetest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

 

 

 

 

 

​검은색 부분이 추가 부분이며 핵심 부분이 되겠습니다 . 

name 은 Service 만든 패키지와 클래스 명을 적으면 되겠습니다 .

 

 

3. Service 명시적 등록 및 시작


마지막으로 이제 Service 를 호출해주면 되는데 Manifast 에 등록한 name 가지고 암시적 으로
Service 등록 및 실행을 할 수 있지만 , 구별하는데 불편함이 있고 , 경고 에러가 나타나기 때문에

명시적 등록 을 습관 들이는 것이 좋을 것 같습니다 .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.example.servicetest;
 
import com.example.service.TestService;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
 
public class MainActivity extends Activity {
 
    private Intent intent ; 
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    }
    
    public void Start(View view){
        // 명시적 등록
        intent = new Intent(MainActivity.this, TestService.class);
        //서비스 시작
        startService(intent);
    }
    
    public void End(View view){
        //서비스 종료
        stopService(intent);
    }
 
}
 

 

 

​위 코드도 정말 명시적 이여서 설명은 주석으로 대체 하겠습니다 . 

 


Comments