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


相关推荐

  • Tle格式

    Tle格式TLE 格式 nbsp CelesTrak 提供

    2026年3月20日
    2
  • clipper使用

    clipper使用一、clipper使用的redis库说明enumRedisDBTable{REDIS_STATE_DB_NUM=1,REDIS_MODEL_DB_NUM=2,REDIS_CONTAINER_DB_NUM=3,REDIS_RESOURCE_DB_NUM=4,REDIS_APPLICATION_DB_NUM=5,REDIS_METADATA_DB_NUM=6,//usedtostoreClipperconfigurationmetadat

    2025年8月23日
    11
  • 关于cfg80211

    关于cfg80211转至 http blog csdn net robertsong20 article details 关于 cfg80211cfg8 是 Linux802 11 配置 API cfg80211 用于取代 Wireless Extensions nl80211 用来配置一个 cfg80211 设备 用于内核用户空间之间的通信 Wi

    2026年3月17日
    3
  • SSM-Mybatis(3)[通俗易懂]

    SSM-Mybatis(3)[通俗易懂]复杂的sql查询环境搭建CREATE TABLE `teacher` ( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8INSERT INTO teacher(id, name) VALUES (1,’秦老师’); CREATE TABLE `student` ( `id` INT(10) NOT NULL

    2022年8月8日
    7
  • 工作流管理系统简介

    工作流管理系统简介上篇文章介绍了工作流的概念和作用 这篇文章继续介绍根据工作流运转原理产生的具体工作流管理系统 nbsp nbsp nbsp nbsp 因为有许多软件开发商都有工作流产品 并且不断有新的工作流产品走入市场 市场上可选择的产品范围很大 因此每个开发商只关注产品的特殊功能 而用户可以采用不同的产品来满足不同的需求 nbsp nbsp nbsp nbsp 然而 由于各个厂商不兼容的流程控制方式 导致没有统一的规范使得不同的工作流产品协同工作

    2026年3月16日
    3
  • java 僵尸进程_僵尸进程ZOMBIE

    java 僵尸进程_僵尸进程ZOMBIE僵尸进程是指它的父进程已经退出 父进程没有等待 调用 wait waitpid 它 而该进程 dead 之后没有进程接受 就成为僵尸进程 也就是 zombie 进程 一个进程在调用 exit 命令结束自己的生命的时候 其实它并没有真正的被销毁 而是留下一个称为僵尸进程 Zombie 的数据结构 系统调用 exit 它的作用是使进程退出 但也仅仅限于将一个正常的进程变成一个僵尸进程 并不能将其完全销毁 在 L

    2026年3月17日
    1

发表回复

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

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