SyncService.java (2073B)
1 package de.nordgedanken.rpicms.basicsyncadapter; 2 3 /** 4 * Created by frank on 13.10.14. 5 */ 6 import android.app.Service; 7 import android.content.Intent; 8 import android.os.IBinder; 9 import android.util.Log; 10 public class SyncService extends Service { 11 12 13 /** Service to handle sync requests. 14 * 15 * <p>This service is invoked in response to Intents with action android.content.SyncAdapter, and 16 * returns a Binder connection to SyncAdapter. 17 * 18 * <p>For performance, only one sync adapter will be initialized within this application's context. 19 * 20 * <p>Note: The SyncService itself is not notified when a new sync occurs. It's role is to 21 * manage the lifecycle of our {@link SyncAdapter} and provide a handle to said SyncAdapter to the 22 * OS on request. 23 */ 24 25 private static final String TAG = "SyncService"; 26 27 private static final Object sSyncAdapterLock = new Object(); 28 private static SyncAdapter sSyncAdapter = null; 29 30 /** 31 * Thread-safe constructor, creates static {@link SyncAdapter} instance. 32 */ 33 @Override 34 public void onCreate() { 35 super.onCreate(); 36 Log.i(TAG, "Service created"); 37 synchronized (sSyncAdapterLock) { 38 if (sSyncAdapter == null) { 39 sSyncAdapter = new SyncAdapter(getApplicationContext(), true); 40 } 41 } 42 } 43 44 @Override 45 /** 46 * Logging-only destructor. 47 */ 48 public void onDestroy() { 49 super.onDestroy(); 50 Log.i(TAG, "Service destroyed"); 51 } 52 53 /** 54 * Return Binder handle for IPC communication with {@link SyncAdapter}. 55 * 56 * <p>New sync requests will be sent directly to the SyncAdapter using this channel. 57 * 58 * @param intent Calling intent 59 * @return Binder handle for {@link SyncAdapter} 60 */ 61 @Override 62 public IBinder onBind(Intent intent) { 63 return sSyncAdapter.getSyncAdapterBinder(); 64 } 65 66 }