안드로이드 sync 를 사용하기 위해서는 계정접근을 위한 Service 와 sync 를 위한 Service 가 필요합니다.

 

 

1. 계정 접근을 위한 Service

   - 아래와 같은 형태로 Service 를 추가하게 됩니다.

<service
    android:name=".authenticator.AuthenticationService"
    android:exported="true">
    <intent-filter>
        <action
            android:name="android.accounts.AccountAuthenticator" />
    </intent-filter>
    <meta-data
        android:name="android.accounts.AccountAuthenticator"
        android:resource="@xml/authenticator" />
</service>

 

   - 위와 같은 형태로 서비스를 등록하면 안드로이드 설정메뉴중 “Accounts & Sync” 메뉴에 자동으로 등록됩니다.image

 

   - 여기 나오는 메뉴의 형태는 @xml/authenticator 에서 정의합니다.

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="com.example.android.samplesync"
    android:icon="@drawable/icon"
    android:smallIcon="@drawable/icon"
    android:label="@string/label"
/>

 

  - 로그인후 받은 토큰을 내부에 저장하고 다음번 로그인시에 그 토큰으로 자동으로 로그인합니다.

 

 

 

2. Sync 를 위한 Service

  - 아래와 같은 형태로 Service 를 추가하게 됩니다.

<service
    android:name=".syncadapter.SyncService"
    android:exported="true">
    <intent-filter>
        <action
            android:name="android.content.SyncAdapter" />
    </intent-filter>
    <meta-data
        android:name="android.content.SyncAdapter"
        android:resource="@xml/syncadapter" />
    <meta-data
        android:name="android.provider.CONTACTS_STRUCTURE"
        android:resource="@xml/contacts" />
</service>

 

   - 싱크가 실행될때 위의 서비스가 자동으로 호출되게 됩니다.

   - 싱크는 저장된 토큰으로 외부와 통신해서 업데이트된 데이터를 받아와서 내부에 맞게 가공해서 업데이트 하도록 합니다.

by cranix 2010. 12. 20. 11:39