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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • [知识图谱实战篇] 七.HTML+D3实现关系图谱搜索功能

    [知识图谱实战篇] 七.HTML+D3实现关系图谱搜索功能前面作者讲解了很多知识图谱原理知识,包括知识图谱相关技术、Neo4j绘制关系图谱等,但仍缺少一个系统全面的实例。为了加深自己对知识图谱构建的认识,为后续创建贵州旅游知识图谱打下基础,作者深入学习了张宏伦老师的网易云课程,并结合自己的理解和技术分享了该系列专栏,从数据采集、数据展示、数据分析到知识图谱构建,文章后续还会讲解中文数据的实体识别、关系抽取、知识计算等。前面通过六篇文章基本构建了电影知识图谱,并且能显示选中节点相关联的边及属性。

    2022年6月26日
  • 独特的微信号_uniapp和原生小程序混合开发

    独特的微信号_uniapp和原生小程序混合开发flyio是什么?一个支持所有JavaScript运行环境的基于Promise的、支持请求转发、强大的http请求库。可以让您在多个端上尽可能大限度的实现代码复用。flyio的平台支持?目前Fly.js支持的平台包括:Node.js、微信小程序、Weex、ReactNative、QuickApp和浏览器,这些平台的JavaScrip…

    2022年9月4日
  • android系统开机画面_Android开机画面

    android系统开机画面_Android开机画面制作android开机画面AndroidSplashScreenisthefirstscreenvisibletotheuserwhentheapplication’slaunched.Splashscreenisoneofthemostvitalscreensintheapplicationsinceit’stheuser’sfirs…

    2022年5月14日
  • 树莓派命令连接wifi_使用命令行设置树莓派的wifi网络「建议收藏」

    树莓派命令连接wifi_使用命令行设置树莓派的wifi网络「建议收藏」如果你没有登录到常用的图形用户界面,这种方法就适合用来设置树莓派的wifi。尤其是在你没有屏幕或者有线网络,仅使用串口控制线的时候。另外,这种方法也不需要额外的软件,所有需要的东西都已经包含进了树莓派。GETTINGWIFINETWORKDETAILS(获取wifi网络详情)为了扫描wifi网络,可以使用sudoiwlistwlan0scan命令。这个命令会列出所有可使用的wifi网络…

    2022年5月5日
  • shell语法简单介绍

    shell语法简单介绍

    2021年12月10日
  • pycharm怎么逐步调试_pycharm的debug失效

    pycharm怎么逐步调试_pycharm的debug失效前言Debug调试,是一项学习编程人员的重要技能。只有当你学会debug了以后,才可以正确的知道程序的走向流程是如何的,今天就来给大家介绍一下pycharm中的debug功能!debug的前世在正式讲解之前,先来了解下debug这个词的由来,就像我们初学Python时,先要了解下它历史的由来。1937年,美国青年霍华德·艾肯找到IBM公司为其投资200万美元研制计算机,第一台成品…

    2022年8月26日

发表回复

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

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