Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
BasicSyncAdapter / src / com.example.android.basicsyncadapter /

SyncService.java

       
        1
       
       
        /*
       
       
        2
       
       
        * Copyright 2013 Google Inc.
       
       
        3
       
       
        *
       
       
        4
       
       
        * Licensed under the Apache License, Version 2.0 (the "License");
       
       
        5
       
       
        * you may not use this file except in compliance with the License.
       
       
        6
       
       
        * You may obtain a copy of the License at
       
       
        7
       
       
        *
       
       
        8
       
       
        *      http://www.apache.org/licenses/LICENSE-2.0
       
       
        9
       
       
        *
       
       
        10
       
       
        * Unless required by applicable law or agreed to in writing, software
       
       
        11
       
       
        * distributed under the License is distributed on an "AS IS" BASIS,
       
       
        12
       
       
        * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
       
        13
       
       
        * See the License for the specific language governing permissions and
       
       
        14
       
       
        * limitations under the License.
       
       
        15
       
       
        */
       
       
        16
       
       
       
       
        17
       
       
        package com.example.android.basicsyncadapter;
       
       
        18
       
       
       
       
        19
       
       
        import android.app.Service;
       
       
        20
       
       
        import android.content.Intent;
       
       
        21
       
       
        import android.os.IBinder;
       
       
        22
       
       
        import android.util.Log;
       
       
        23
       
       
       
       
        24
       
       
        /** Service to handle sync requests.
       
       
        25
       
       
        *
       
       
        26
       
       
        * <p>This service is invoked in response to Intents with action android.content.SyncAdapter, and
       
       
        27
       
       
        * returns a Binder connection to SyncAdapter.
       
       
        28
       
       
        *
       
       
        29
       
       
        * <p>For performance, only one sync adapter will be initialized within this application's context.
       
       
        30
       
       
        *
       
       
        31
       
       
        * <p>Note: The SyncService itself is not notified when a new sync occurs. It's role is to
       
       
        32
       
       
        * manage the lifecycle of our {@link SyncAdapter} and provide a handle to said SyncAdapter to the
       
       
        33
       
       
        * OS on request.
       
       
        34
       
       
        */
       
       
        35
       
       
        public class SyncService extends Service {
       
       
        36
       
       
        private static final String TAG = "SyncService";
       
       
        37
       
       
       
       
        38
       
       
        private static final Object sSyncAdapterLock = new Object();
       
       
        39
       
       
        private static SyncAdapter sSyncAdapter = null;
       
       
        40
       
       
       
       
        41
       
       
        /**
       
       
        42
       
       
        * Thread-safe constructor, creates static {@link SyncAdapter} instance.
       
       
        43
       
       
        */
       
       
        44
       
       
        @Override
       
       
        45
       
       
        public void onCreate() {
       
       
        46
       
       
        super.onCreate();
       
       
        47
       
       
        Log.i(TAG, "Service created");
       
       
        48
       
       
        synchronized (sSyncAdapterLock) {
       
       
        49
       
       
        if (sSyncAdapter == null) {
       
       
        50
       
       
        sSyncAdapter = new SyncAdapter(getApplicationContext(), true);
       
       
        51
       
       
        }
       
       
        52
       
       
        }
       
       
        53
       
       
        }
       
       
        54
       
       
       
       
        55
       
       
        @Override
       
       
        56
       
       
        /**
       
       
        57
       
       
        * Logging-only destructor.
       
       
        58
       
       
        */
       
       
        59
       
       
        public void onDestroy() {
       
       
        60
       
       
        super.onDestroy();
       
       
        61
       
       
        Log.i(TAG, "Service destroyed");
       
       
        62
       
       
        }
       
       
        63
       
       
       
       
        64
       
       
        /**
       
       
        65
       
       
        * Return Binder handle for IPC communication with {@link SyncAdapter}.
       
       
        66
       
       
        *
       
       
        67
       
       
        * <p>New sync requests will be sent directly to the SyncAdapter using this channel.
       
       
        68
       
       
        *
       
       
        69
       
       
        * @param intent Calling intent
       
       
        70
       
       
        * @return Binder handle for {@link SyncAdapter}
       
       
        71
       
       
        */
       
       
        72
       
       
        @Override
       
       
        73
       
       
        public IBinder onBind(Intent intent) {
       
       
        74
       
       
        return sSyncAdapter.getSyncAdapterBinder();
       
       
        75
       
       
        }
       
       
        76
       
       
        }