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)
上一篇 2022年2月3日 上午8:00
下一篇 2022年2月3日 上午8:00


相关推荐

  • win10电脑一插u盘就重启_u盘启动盘复制到另一个u盘

    win10电脑一插u盘就重启_u盘启动盘复制到另一个u盘不知道大家有没有遇到过这样的情况,就是当你用U盘复制东西的时候,电脑莫名其妙自动重启,可是只要讲U盘拔下来就没事了,而用杀毒软件杀毒也没有查出任何病毒,重新安装了一次系统也是一样的无济于事,这究竟是什么问题?带着这个问题往下看,U盘量产网将为你解答。这个问题几率不大,但首先要百分之百确认U盘里没有病毒或者木马,杀毒软件可能检查不出来,所以建议您将U盘格式化,格式化时注意要选择FAT而不是FAT32…

    2025年10月9日
    4
  • ViewPager 全面总结

    ViewPager 全面总结一、简介Viewpager,视图翻页工具,提供了多页面切换的效果。Android3.0后引入的一个UI控件,位于v4包中。低版本使用需要导入v4包,但是现在我们开发的APP一般不再兼容3.0及以下的系统版本,另外现在大多数使用Androidstudio进行开发,默认导入v7包,v7包含了v4,所以不用导包,越来越方便了。Viewpager使用起来就是我们通过创建adapter给它填充多…

    2022年7月22日
    17
  • 字符串类的设计与实现_C语言字符串编程题

    字符串类的设计与实现_C语言字符串编程题Java程序设计(基础)- 字符串

    2022年4月22日
    44
  • 二部图(二分图)总结

    二部图(二分图)总结1 二部图二部图又叫二分图 是图论中的一种特殊模型 设 G V E 是一个无向图 如果顶点 V 可分割为两个互不相交的子集 A B 并且图中的每条边 i j 所关联的两个顶点 i 和 j 分别属于这两个不同的顶点集 iinA jinB 则称图 G 为一个二分图 简单来说 如果图中点可以被分为两组 并且使得所有边都跨越组的边界 则这就是一个二分图 准确地说 把一个图的顶点划分为两个不相交子集 使得

    2026年3月19日
    3
  • mybatis分页查询之sql server–mysql[通俗易懂]

    mybatis分页查询之sql server–mysql[通俗易懂]freemarker.beansKey”location”wasnotfoundoninstanceoforg.springframework.jdbc.UncategorizedSQLException.freemarker.beansKey”location”wasnotfoundoninstanceofcom.microsoft.sqlserver.jdbc.SQLServerException.sqlserver使用mybatis中分页查询时出现故障

    2022年5月12日
    43
  • Wireshark 过滤 规则

    Wireshark 过滤 规则在做网络的开发中我们必不可少的工具就是 wireshark 他能抓到链路层以上的包 在海量的 packets 中我们如何快速的找到自己想要看的包 例如根据 protocol tcpudparpigm 等这些写有从链路到应用层 这里我们有一个小的技巧需要根据那一层的标记去过滤就在 wiresharkfil 上 ip 开始就会 list 出能支持的过滤项 例如需要过滤 ip 链路层关键字过滤传输层 链路层含有另一个

    2026年3月18日
    2

发表回复

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

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