0

Transferring Data Using Sync Adapters in android

Việc đồng bộ dữ liệu giữa các thiết bị android và web server trong ứng dụng android là một vấn đề rất quang trọng trong việc phát triển ứng dụng android tuơng tác với web server. Nếu có thể xử lý tốt vấn đề này chúng ta sẽ tạo nên một trải nghiệm tuyệt vời cho ngừời dùng, dữ liệu luôn luôn có sẵn để người dùng có thể sử dụng, ngay cả khi thiết bị android đang offline. Một ví dụ điển hình cho việc transferring data đó là ứng dụng google calendar, chúng ta có thể tạo sự kiện khi thiết bị mobile không có kết nối internet và khi được kết nối network nó sẽ tự động gửi dữ liệu lên server.

Có rất nhiều cách để chúng ta có thể implement transferring data. Trong bài viết hôm nay tôi sẽ giới thiệu với các bạn một framework để được sử dụng cho việc đồng bộ dữ liệu đó là Android's sync adapter framework

  1. Giới thiệu về Android's sync adapter framework

Android's sync adapter framework là một framework giúp cho việc quản lý và tự động đồng bộ dữ liệu giữa các ứng dụng khác nhau. Nó có các tính năng đặc trưng sau:

  • Cho phép bạn thêm data transfer code vào hệ thống.
  • Cho phép tự động chuyển dữ liệu theo lịch mà chúng ta đặt ra.
  • Tự động kiểm tra kết nối mạng và chỉ thực hiện data transfer khi có kết nối.
  • Quản lý tài khoản và việc xác thực tài khoản(khi kết nối đến server yêu cầu login hay xác thực tài khoản)
  1. Implement Android's sync adapter framework

Để implement Android's sync adapter framework vào ứng dụng của mình thường sẽ phải thực hiện 4 bước sau đây:

  • Creating a Stub Authenticator
  • Creating a Stub Content Provider
  • Creating a Sync Adapter
  • Running a Sync Adapter

a. Creating a Stub Authenticator

Sync Adapter Framework yêu cầu chúng ta cung cấp một Authenticator như là một điều bắt buộc framwork mặc định hiểu bạn đang tuơng tác với một server yêu cầu login nên cần phải có một tài khoản xác thực. Để cung cấp một authenticator chúng ta cần hiện thực hóa lớp AbstractAccountAuthenticator.

/*
 * Implement AbstractAccountAuthenticator and stub out all
 * of its methods
 */
public class Authenticator extends AbstractAccountAuthenticator {
    // Simple constructor
    public Authenticator(Context context) {
        super(context);
    }
    // Editing properties is not supported
    @Override
    public Bundle editProperties(
            AccountAuthenticatorResponse r, String s) {
        throw new UnsupportedOperationException();
    }
    // Don't add additional accounts
    @Override
    public Bundle addAccount(
            AccountAuthenticatorResponse r,
            String s,
            String s2,
            String[] strings,
            Bundle bundle) throws NetworkErrorException {
        return null;
    }
    // Ignore attempts to confirm credentials
    @Override
    public Bundle confirmCredentials(
            AccountAuthenticatorResponse r,
            Account account,
            Bundle bundle) throws NetworkErrorException {
        return null;
    }
    // Getting an authentication token is not supported
    @Override
    public Bundle getAuthToken(
            AccountAuthenticatorResponse r,
            Account account,
            String s,
            Bundle bundle) throws NetworkErrorException {
        throw new UnsupportedOperationException();
    }
    // Getting a label for the auth token is not supported
    @Override
    public String getAuthTokenLabel(String s) {
        throw new UnsupportedOperationException();
    }
    // Updating user credentials is not supported
    @Override
    public Bundle updateCredentials(
            AccountAuthenticatorResponse r,
            Account account,
            String s, Bundle bundle) throws NetworkErrorException {
        throw new UnsupportedOperationException();
    }
    // Checking features for the account is not supported
    @Override
    public Bundle hasFeatures(
        AccountAuthenticatorResponse r,
        Account account, String[] strings) throws NetworkErrorException {
        throw new UnsupportedOperationException();
    }
}

Nếu muốn Sync Adapter Framework có thể truy cập được authenticator chúng ta cần tạo một bound service, service này sẽ cung cấp một binder object để truy cập và chuyển data giữa framework và authenticator

/**
 * A bound Service that instantiates the authenticator
 * when started.
 */
public class AuthenticatorService extends Service {
    ...
    // Instance field that stores the authenticator object
    private Authenticator mAuthenticator;
    @Override
    public void onCreate() {
        // Create a new authenticator object
        mAuthenticator = new Authenticator(this);
    }
    /*
     * When the system binds to this Service to make the RPC call
     * return the authenticator's IBinder.
     */
    @Override
    public IBinder onBind(Intent intent) {
        return mAuthenticator.getIBinder();
    }
}

'''

Cuối cùng là tạo một file metadata và khai báo service trong manifest

```metadata

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:accountType="example.com"
        android:icon="@drawable/ic_launcher"
        android:smallIcon="@drawable/ic_launcher"
        android:label="@string/app_name"/>


<service
            android:name="com.example.android.syncadapter.AuthenticatorService">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator"/>
        </intent-filter>
        <meta-data
            android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>

b. Creating a Stub Content Provider

Để đồng bộ dữ liệu với server phía mobile cũng phải có một cơ sở dữ liệu tuơng tự như phía server và Sync data framework yêu cầu bạn phải sử dụng Content provider để quản lý truy vấn dữ liệu từ cơ sở dữ liệu phía mobile

Để tạo một content provider bạn tạo một lớp extends từ class ContentProvider và cài đặt các hàm truy vấn và quản lý data .


/*
 * Define an implementation of ContentProvider that stubs out
 * all methods
 */
public class StubProvider extends ContentProvider {
    /*
     * Always return true, indicating that the
     * provider loaded correctly.
     */
    @Override
    public boolean onCreate() {
        return true;
    }
    /*
     * Return no type for MIME type
     */
    @Override
    public String getType(Uri uri) {
        return null;
    }
    /*
     * query() always returns no results
     *
     */
    @Override
    public Cursor query(
            Uri uri,
            String[] projection,
            String selection,
            String[] selectionArgs,
            String sortOrder) {
        return null;
    }
    /*
     * insert() always returns null (no URI)
     */
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }
    /*
     * delete() always returns "no rows affected" (0)
     */
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }
    /*
     * update() always returns "no rows affected" (0)
     */
    public int update(
            Uri uri,
            ContentValues values,
            String selection,
            String[] selectionArgs) {
        return 0;
    }
}

Khai báo content provider được định nghĩa trong manifest


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.network.sync.BasicSyncAdapter"
    android:versionCode="1"
    android:versionName="1.0" >
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    ...
    <provider
        android:name="com.example.android.datasync.provider.StubProvider"
        android:authorities="com.example.android.datasync.provider"
        android:exported="false"
        android:syncable="true"/>
    ...
    </application>
</manifest>

c. Creating a Sync Adapter

Hai bước trên chúng ta đã tạo thành công Stub Authenticator và ContentProvider đến bước này chúng ta sẽ tạo một Sync Adapter thực hiện việc transfer data.

Tạo một Sync Adapter bằng cách hiện thực hóa lớp AbstractThreadedSyncAdapter


/**
 * Handle the transfer of data between a server and an
 * app, using the Android sync adapter framework.
 */
public class SyncAdapter extends AbstractThreadedSyncAdapter {
    ...
    // Global variables
    // Define a variable to contain a content resolver instance
    ContentResolver mContentResolver;
    /**
     * Set up the sync adapter
     */
    public SyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
        /*
         * If your app uses a content resolver, get an instance of it
         * from the incoming Context
         */
        mContentResolver = context.getContentResolver();
    }
    ...
    /**
     * Set up the sync adapter. This form of the
     * constructor maintains compatibility with Android 3.0
     * and later platform versions
     */
    public SyncAdapter(
            Context context,
            boolean autoInitialize,
            boolean allowParallelSyncs) {
        super(context, autoInitialize, allowParallelSyncs);
        /*
         * If your app uses a content resolver, get an instance of it
         * from the incoming Context
         */
        mContentResolver = context.getContentResolver();

    }

     /*
     * Specify the code you want to run in the sync adapter. The entire
     * sync adapter runs in a background thread, so you don't have to set
     * up your own background processing.
     */
    @Override
    public void onPerformSync(
            Account account,
            Bundle extras,
            String authority,
            ContentProviderClient provider,
            SyncResult syncResult) {
    /*
     * Put the data transfer code here.
     */
    ...
    }

Thực hiện việc transfer data trong hàm onPerformSync

Cũng tuơng tự như phần tạo stub authenticator bạn cần tạo một bound service để framework có thể truy cập được đến sync adapter và gọi hàm onPerformSync() .


package com.example.android.syncadapter;
/**
 * Define a Service that returns an IBinder for the
 * sync adapter class, allowing the sync adapter framework to call
 * onPerformSync().
 */
public class SyncService extends Service {
    // Storage for an instance of the sync adapter
    private static SyncAdapter sSyncAdapter = null;
    // Object to use as a thread-safe lock
    private static final Object sSyncAdapterLock = new Object();
    /*
     * Instantiate the sync adapter object.
     */
    @Override
    public void onCreate() {
        /*
         * Create the sync adapter as a singleton.
         * Set the sync adapter as syncable
         * Disallow parallel syncs
         */
        synchronized (sSyncAdapterLock) {
            if (sSyncAdapter == null) {
                sSyncAdapter = new SyncAdapter(getApplicationContext(), true);
            }
        }
    }
    /**
     * Return an object that allows the system to invoke
     * the sync adapter.
     *
     */
    @Override
    public IBinder onBind(Intent intent) {
        /*
         * Get the object that allows external processes
         * to call onPerformSync(). The object is created
         * in the base class code when the SyncAdapter
         * constructors call super()
         */
        return sSyncAdapter.getSyncAdapterBinder();
    }
}

Tiếp theo chúng ta cần add một account vào hệ thống( đây là bước bắt buộc)


public class MainActivity extends FragmentActivity {
    ...
    ...
    // Constants
    // The authority for the sync adapter's content provider
    public static final String AUTHORITY = "com.example.android.datasync.provider";
    // An account type, in the form of a domain name
    public static final String ACCOUNT_TYPE = "example.com";
    // The account name
    public static final String ACCOUNT = "dummyaccount";
    // Instance fields
    Account mAccount;
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        // Create the dummy account
        mAccount = CreateSyncAccount(this);
        ...
    }
    ...
    /**
     * Create a new dummy account for the sync adapter
     *
     * @param context The application context
     */
    public static Account CreateSyncAccount(Context context) {
        // Create the account type and default account
        Account newAccount = new Account(
                ACCOUNT, ACCOUNT_TYPE);
        // Get an instance of the Android account manager
        AccountManager accountManager =
                (AccountManager) context.getSystemService(
                        ACCOUNT_SERVICE);
        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (accountManager.addAccountExplicitly(newAccount, null, null)) {
            /*
             * If you don't set android:syncable="true" in
             * in your <provider> element in the manifest,
             * then call context.setIsSyncable(account, AUTHORITY, 1)
             * here.
             */
        } else {
            /*
             * The account exists or some other error occurred. Log this, report it,
             * or handle it internally.
             */
        }
    }
    ...
}

Tạo metadata file và khai báo trong manifest

```metadata
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:contentAuthority="com.example.android.datasync.provider"
        android:accountType="com.android.example.datasync"
        android:userVisible="false"
        android:supportsUploading="false"
        android:allowParallelSyncs="false"
        android:isAlwaysSyncable="true"/>

<service
                android:name="com.example.android.datasync.SyncService"
                android:exported="true"
                android:process=":sync">
            <intent-filter>
                <action android:name="android.content.SyncAdapter"/>
            </intent-filter>
            <meta-data android:name="android.content.SyncAdapter"
                    android:resource="@xml/syncadapter" />
        </service>

d. Chạy SyncAdapter

Sau khi hoàn thành các bước trên việc cuối cùng chúng ta cần làm đó là chạy SyncAdapter. khi bạn implement framework này nó cho phép bạn chạy sync dapter để thực hiện đồng bộ khi dữ liệu phía local thay đổi, dữ liệu server thay đổi hoặc theo lịch mà bạn lặp sẵn .và bạn sẽ xem xét thực hiện việc đồng bộ dữ liệu như thế nào sẽ tốt trong ứng dụng của mình . mình sẽ chỉ đưa ra một ví dụ về chạy đồng bộ dữ liệu khi dữ liệu server thay đổi và server gửi thông báo qua GCM:

public class GcmBroadcastReceiver extends BroadcastReceiver {
    ...
    // Constants
    // Content provider authority
    public static final String AUTHORITY = "com.example.android.datasync.provider"
    // Account type
    public static final String ACCOUNT_TYPE = "com.example.android.datasync";
    // Account
    public static final String ACCOUNT = "default_account";
    // Incoming Intent key for extended data
    public static final String KEY_SYNC_REQUEST =
            "com.example.android.datasync.KEY_SYNC_REQUEST";
    ...
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get a GCM object instance
        GoogleCloudMessaging gcm =
                GoogleCloudMessaging.getInstance(context);
        // Get the type of GCM message
        String messageType = gcm.getMessageType(intent);
        /*
         * Test the message type and examine the message contents.
         * Since GCM is a general-purpose messaging system, you
         * may receive normal messages that don't require a sync
         * adapter run.
         * The following code tests for a a boolean flag indicating
         * that the message is requesting a transfer from the device.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)
            &&
            intent.getBooleanExtra(KEY_SYNC_REQUEST)) {
            /*
             * Signal the framework to run your sync adapter. Assume that
             * app initialization has already created the account.
             */
            ContentResolver.requestSync(ACCOUNT, AUTHORITY, null);
            ...
        }
        ...
    }
    ...
}

lưu ý:

  • acountType mà các bạn định nghĩa trong các file metadata và khai báo trong manifest và khi tạo account là một một chuỗi bất kì do các bạn tự định nghĩa nhưng yêu cầu nó phải giống nhau .

Example: https://github.com/Nghicv/demo-syncadapter


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí