【Android源码 栏目提醒】:网学会员在Android源码 频道为大家收集整理了“[SampleSyncAdapter工程][android源码][syncadapter-SyncAdapter java] - 开发文档“提供大家参考,希望对大家有所帮助!
/ Copyright C 2010 The Android Open Source Project Licensed 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. / package com.example.android.samplesync.syncadapter import android.accounts.Account import android.accounts.AccountManager import android.accounts.AuthenticatorException import android.accounts.OperationCanceledException import android.content.AbstractThreadedSyncAdapter import android.content.ContentProviderClient import android.content.Context import android.content.SyncResult import android.os.Bundle import android.util.Log import com.example.android.samplesync.Constants import com.example.android.samplesync.client.NetworkUtilities import com.example.android.samplesync.client.User import com.example.android.samplesync.client.User.Status import com.example.android.samplesync.platform.ContactManager import org.apache.http.ParseException import org.apache.http.auth.AuthenticationException import org.json.JSONException import java.io.IOException import java.util.Date import java.util.List / SyncAdapter implementation for syncing sample SyncAdapter contacts to the platform ContactOperations provider. / public class SyncAdapter extends AbstractThreadedSyncAdapter private static final String TAG SyncAdapter private final AccountManager mAccountManager private final Context mContext private Date mLastUpdated public SyncAdapterContext context boolean autoInitialize supercontext autoInitialize mContext context mAccountManager AccountManager.getcontext Override public void onPerformSyncAccount account Bundle extras String authority ContentProviderClient provider SyncResult syncResult List users List statuses String authtoken null try // use the account manager to request the credentials authtoken mAccountManager .blockingGetAuthTokenaccount Constants.AUTHTOKEN_TYPE true / notifyAuthFailure / // fetch updates from the sample service over the cloud users NetworkUtilities.fetchFriendUpdatesaccount authtoken mLastUpdated // update the last synced date. mLastUpdated new Date // update platform contacts. Log.dTAG Calling contactManagers sync contacts ContactManager.syncContactsmContext account.name users // fetch and update status messages for all the synced users. statuses NetworkUtilities.fetchFriendStatusesaccount authtoken ContactManager.insertStatusesmContext account.name statuses catch final AuthenticatorException e syncResult.stats.numParseExceptions Log.eTAG AuthenticatorException e catch final OperationCanceledException e Log.eTAG OperationCanceledExcetpion e catch final IOException e Log.eTAG IOException e syncResult.stats.numIoExceptions catch final AuthenticationException e mAccountManager.invalidateAuthTokenConstants.ACCOUNT_TYPE authtoken syncResult.stats.numAuthExceptions Log.eTAG AuthenticationException e catch final ParseException e syncResult.stats.numParseExceptions Log.eTAG ParseException e catch final JSONException e syncResult.stats.numParseExceptions Log.eTAG JSONException e