android账号与同步之同步实现

android账号与同步之同步实现

大家好,又见面了,我是全栈君。

上一篇博文我先介绍了账号与同步的账号管理,这篇就介绍一下还有一部分。就是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()将会中断同步操作。

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115533.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python贪吃蛇游戏代码详解外加中文_Python贪吃蛇代码

    python贪吃蛇游戏代码详解外加中文_Python贪吃蛇代码#!/usr/bin/envpythonimportpygame,sys,time,randomfrompygame.localsimport*#定义颜色变量redColour=pygame.Color(255,0,0)blackColour=pygame.Color(0,0,0)whiteColour=pygame.Color(255,255,255)greyColour…

    2022年8月10日
    12
  • 微信公众号高质量技术贴-提炼总结

    微信公众号高质量技术贴-提炼总结微信公众号高质量技术贴过滤掉对自己感觉没有技术相关性的,或者是那种水贴对内容进行归类整理阅读完写下自己的读后感LINUX从无盘启动看Linux启动原理“只读内存”(ROM)—-“基本输入输出系统”(BIOS)—-“硬件自检”(POST)—-“启动顺序”(BootSequence)上电自检—-UEFI固件被加载—-加载UEFI应用—-启动内核及initramfs/sbin/init—-/etc/inittab—-etc/rcN.dLi

    2022年7月19日
    23
  • 前端写接口 请求后台数据 存vuex中 打印到控制台「建议收藏」

    前端写接口 请求后台数据 存vuex中 打印到控制台「建议收藏」前端写接口请求后台数据,ajaxaxiosvuex数据处理,数据渲染。

    2022年8月30日
    9
  • 盘点当下大热的 7 大 Github 机器学习『创新』项目

    盘点当下大热的 7 大 Github 机器学习『创新』项目本文将会分享近期发布的七大GitHub机器学习项目。这些项目广泛覆盖了机器学习的各个领域,包括自然语言处理(NLP)、计算机视觉、大数据等。最顶尖的Github机器学习项…

    2022年6月7日
    36
  • shell if参数-v

    shell if参数-v先说结论:if参数-v可用于判断变量是否存在,即该变量是否已定义示例代码如下#!bin/bash#targs=0#runsim=”if[-vtargs];thenif[-vrunsim];then echo”LayerONE”fiecho”LayerTWO”fiecho”LayerThree”直接运行结果为LayerThree将第二行注释去除后,即定义变量targs=0,在此运行该脚本,结果如下Laye

    2022年7月11日
    16
  • C语言-链表排序_单链表的排序c语言

    C语言-链表排序_单链表的排序c语言C语言-链表排序题目描述已有a、b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。输入第一行,a、b两个链表元素的数量N、M,用空格隔开。接下来N行是a的数据然后M行是b的数据每行数据由学号和成绩两部分组成输出按照学号升序排列的数据样例输入235100689382495210样例输出210382…

    2022年10月11日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号