上一篇博文我先介绍了账号与同步的账号管理,这篇就介绍一下还有一部分。就是android给提供的sync同步机制的使用。

事实上sync机制的使用和上一篇博文中介绍的账号管理非常类似,也是基于binder机制的跨进程通信。首先它须要一个Service。这个服务提供一个Action给系统以便系统能找到它。然后就是继承和实现AbstractThreadedSyncAdapter。此类中包括实现了ISyncAdapter.Stub内部类。这个内部类封装了远程接口调用,这个类getSyncAdapterBinder()方法,返回内部类的IBinder形式,以便对AbstractThreadedSyncAdapte进行远程调用;在manifest中须要对Service注冊,并且指定meta-data。这个meta-data是一个xml文件,在SampleSyncAdapter实例中,它的名字是syncadapter.xml,这个文件指定了账号和被监听的contentprovider。

以下分别介绍这几个文件:

SyncService.java

SyncService是一个继承普通Service的服务,用来给远端进程提供服务,在onBind方法中返回IBinder。

public class SyncService extends Service {

private static final Object sSyncAdapterLock = new Object();

private static SyncAdapter sSyncAdapter = null;

@Override

    public void onCreate() {

        synchronized (sSyncAdapterLock) {

            if (sSyncAdapter == null) {

                sSyncAdapter = new SyncAdapter(getApplicationContext(), true);

            }

        }

    }

@Override

    public IBinder onBind(Intent intent) {

        return sSyncAdapter.getSyncAdapterBinder();

    }

}

它的action取值为android.content.SyncAdapter。注冊例如以下:

 <service

            android:name=".syncadapter.SyncService"

            android:exported="true">

            <intent-filter>

                <action

                    android:name="android.content.SyncAdapter" />

            </intent-filter>

            <meta-data

                android:name="android.content.SyncAdapter"

                android:resource="@xml/syncadapter" />

</service>

一个适配器仅仅能同步一个Authority,若想使一个账户同步多个Authority,能够向系统注冊多个绑定同一账户的sync-adapter。

syncadapter.xml

syncadapter.xml文件指定了此Service所监听的contentprovider的Authority,还指定了监听此Authority的账号类型accountType,这个账号类型和上一篇文章中的账号类型是相关的。

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"

    android:contentAuthority="com.android.contacts"

    android:accountType="com.example.android.samplesync"

    android:supportsUploading="false"

    android:userVisible="true"

/>

android:contentAuthority 指定要同步的ContentProvider在其AndroidManifest.xml文件里有个android:authorities属性。

android:accountType 表示进行同步的账号的类型。

attributes indicate which content authority and for which account types this sync adapter serves.

android:userVisible 设置是否在“设置”中显示

defaults to true and controls whether or not this sync adapter shows up in the Sync Settings screen.

android:supportsUploading 设置是否必须notifyChange通知才干同步

defaults to true and if true an upload-only sync will be requested for all syncadapters associated with an authority whenever that authority's content provider does a notifyChange(android.net.Uri, android.database.ContentObserver, boolean) with syncToNetwork
set to true.

android:allowParallelSyncs 是否支持多账号同一时候同步

defaults to false and if true indicates that the sync adapter can handle syncs for multiple accounts at the same time. Otherwise the SyncManager will wait until the sync adapter is not in use before requesting that it sync an account's data.

android:isAlwaysSyncable 设置全部账号的isSyncable为1

defaults to false and if true tells the SyncManager to intialize the isSyncable state to 1 for that sync adapter for each account that is added.

android:syncAdapterSettingsAction 指定一个能够设置同步的activity的Action。

defaults to null and if supplied it specifies an Intent action of an activity that can be used to adjust the sync adapter's sync settings. The activity must live in the same package as the sync adapter.

SyncAdapter.java

SyncAdapter是继承自抽象类AbstractThreadedSyncAdapter的。它实现了AbstractThreadedSyncAdapter中的方法,例如以下:

@Override

    public void onPerformSync(Account account, Bundle extras, String authority,

        ContentProviderClient provider, SyncResult syncResult) {

//TODO 进行同步操作

    }

AbstractThreadedSyncAdapter内部提供startSync()和cancelSync()两个方法。两个方法主要是被远端系统进程调用。

startSync()将会启动一个线程,通过在该线程中调用

AbstractThreadedSyncAdapter的 onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult) 方法来运行同步操作。所以上面onPerformSync方法中的操作都

是在新线程中运行的。cancelSync()将会中断同步操作。

android账号与同步之同步实现的更多相关文章

  1. android账号与同步之发起同步

    上一篇博文我介绍了账号与同步的同步实现过程,当中提供了一个工系统进程调用的服务,那么这个服务究竟是怎么被启动和使用的呢?这篇博文我就大体梳理一下启动过程. 事实上作为一个一般开发者,我们仅仅要知道要想 ...

  2. android账号与同步之账号管理

    在android提供的sdk中,samples文件夹下有一个叫SampleSyncAdapter的演示样例,它是一个账号与同步的实例,比方Google原始的android手机能够使用Google账号进 ...

  3. 关于simotion建立同步/解除同步的问题

    关于simotion建立同步/解除同步的问题. 问题: [enable gearing][disable gearing][enable camming][disable camming]都是一个过程 ...

  4. Android中Gallery和ImageSwitcher同步自动(滚动)播放图片库

    本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...

  5. Android 访问Android Wear数据层Api——同步Data Items

    Data Items它被用来同步手机和wear数据接口,一个Date Items通常包含以下几个部分: Payload 字节数组.无论你需要设置数据类型,我们同意对象序列化和反序列化,大小不能超过10 ...

  6. Android 9 Pie震撼来袭 同步登陆WeTest

    WeTest 导读 2018年8月7日,Google对外发布最新 Android 9.0 正式版系统,并宣布系统版本Android P 被正式命名为代号“Pie”,最新系统已经正式推送包括谷歌Pixe ...

  7. 安卓Android手机直播推送同步录像功能设计与实现源码

    本文转自:http://blog.csdn.net/jyt0551/article/details/58714595 EasyPusher是一款非常棒的推送客户端.稳定.高效.低延迟,音视频同步等都特 ...

  8. android 删除相册图片并同步到图库

    private void deleteImage(String imgPath) { ContentResolver resolver = getContentResolver(); Cursor c ...

  9. 线程同步 synchronized 同步代码块 同步方法 同步锁

    一 同步代码块 1.为了解决并发操作可能造成的异常,java的多线程支持引入了同步监视器来解决这个问题,使用同步监视器的通用方法就是同步代码块.其语法如下: synchronized(obj){ // ...

随机推荐

  1. Oracle 基础知识

    SQLDevelop 1. 查看数据库版本 :  select  *  from   v$version; 2. 查看表结构:        desc     TABLE_NAME 3. 查看当前连接 ...

  2. NOIP 2011 提高组 计算系数

    有二项式定理 `\left( a+b\right) ^{n}=\sum _{r=0}^{n}\left( \begin{matrix} n\\ r\end{matrix} \right) a^{n-r ...

  3. 修改 Analysis Service 服务器模式

    原网址:http://cathydumas.com/2012/04/23/changing-an-analysis-services-instance-to-tabular-mode/ Say you ...

  4. ALV 行列 颜色

    1)颜色含义 1:海蓝:2:浅清:3:黄色:4:浅蓝:5:青色:6:红色:7:橙色.(1)首位为主颜色:(2)次位为辅助颜色:(3)末位为0时,表示首位数字表为表格的底色:末位为1时,则表示以1为底色 ...

  5. Java使用JAX-WS来写webservice时 Unable to create JAXBContext

    webservice,作为web开发人员来说必须掌握的一门技术,它的好处这里就不多说了,eclipse中自带了一种生成webservice的 方法,使用JAX-WS,如果我没有弄错的话,它需要java ...

  6. HDU 3308 LCIS 线段树区间更新

    最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛  ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...

  7. Google 发布 Android 性能优化典范

    2015年伊始,Google发布了关于Android性能优化典范的专题, 一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有 ...

  8. haskell Types 和 Typeclasses

    Algebraic Data Types 入门 在前面的章节中,我们谈了一些 Haskell 内置的类型和 Typeclass.而在本章中,我们将学习构造类型和 Typeclass 的方法. 我们已经 ...

  9. JMS的作用

    JMS就是生产者与消费者模式.消费者负责消费生产者产生的消息.通过JMS可以做后台的异步操作,应用到具体工作中的话,有用它来发内部消息的.发邮件的.发短信的,做大操作时在后台做异步操作的. Java ...

  10. struts2结合生成验证码

    import java.util.Arrays; /** * 工具类,生成随机验证码字符串 * @version 1.0 2012/12/01 * @author shiyz * */ public ...