Courtesy: GitHub

Android BasicSyncAdapter Sample

This sample demonstrates using SyncAdapter to fetch background data for an app. It covers the creation of the required Service that the OS uses to initiate the background data sync as well as scheduling syncs with background data.

Introduction

This sample demonstrates using SyncAdapter to fetch background data for an app. SyncAdapters can be used to execute your data transfer code at configurable intervals, while efficiently using battery and other system resources.

This sample implements all the required elements of a sync adapter.

  • Creates a sync adapter class.
  • Creates a bound Service which the OS uses to initiate a sync.
  • Defines the sync adapter properties in an XML resource file.
  • Declares the bound Service in the app manifest.

For more on SyncAdapters refer to Transferring Data Using Sync Adapters

Pre-requisites

  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Screenshots

Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-BasicSyncAdapter

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile "com.android.support:support-v4:26.1.0"

compile "com.android.support:gridlayout-v7:26.1.0"

compile "com.android.support:cardview-v7:26.1.0"

compile "com.android.support:appcompat-v7:26.1.0"

compileSdkVersion 26

minSdkVersion 7

targetSdkVersion 26

package com.example.android.basicsyncadapter.tests

versionCode 1

versionName 1.0

package com.example.android.basicsyncadapter

versionCode 1

versionName 1.0

uses-permission

  • android.permission.INTERNET
  • android.permission.READ_SYNC_STATS
  • android.permission.WRITE_SYNC_SETTINGS
  • android.permission.AUTHENTICATE_ACCOUNTS

EntryListActivity

EntryListActivity

Activity for holding EntryListFragment.

SyncService

GenericAccountService

SyncService

Service to handle sync requests. *

This service is invoked in response to Intents with action android.content.SyncAdapter, and returns a Binder connection to SyncAdapter. *

For performance, only one sync adapter will be initialized within this application's context. *

Note: The SyncService itself is not notified when a new sync occurs. It's role is to manage the lifecycle of our {@link SyncAdapter} and provide a handle to said SyncAdapter to the OS on request.

Thread-safe constructor, creates static {@link SyncAdapter} instance.

Logging-only destructor.

Return Binder handle for IPC communication with {@link SyncAdapter}. *

New sync requests will be sent directly to the SyncAdapter using this channel. * @param intent Calling intent @return Binder handle for {@link SyncAdapter}

GenericAccountService

Obtain a handle to the {@link android.accounts.Account} used for sync in this application. *

It is important that the accountType specified here matches the value in your sync adapter configuration XML file for android.accounts.AccountAuthenticator (often saved in res/xml/syncadapter.xml). If this is not set correctly, you'll receive an error indicating that "caller uid XXXXX is different than the authenticator's uid". * @param accountType AccountType defined in the configuration XML file for android.accounts.AccountAuthenticator (e.g. res/xml/syncadapter.xml). @return Handle to application's account (not guaranteed to resolve unless CreateSyncAccount() has been called)

FeedProvider

FeedProvider

Content authority for this provider.

URI ID for route: /entries

URI ID for route: /entries/{ID}

UriMatcher, used to decode incoming URIs.

Determine the mime type for entries returned by a given URI.

Perform a database query by URI. *

Currently supports returning all entries (/entries) and individual entries by ID (/entries/{ID}).

Insert a new entry into the database.

Delete an entry by database by URI.

Update an etry in the database by URI.

SQLite backend for @{link FeedProvider}. * Provides access to an disk-backed, SQLite datastore which is utilized by FeedProvider. This database should never be accessed by other parts of the application directly.

Schema version.

Filename for SQLite file.

SQL statement to create "entry" table.

SQL statement to drop "entry" table.