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


相关推荐

  • 双机热备方案设计

    1什么是双机热备方案  双机热备就是使用互为备份的两台服务器共同执行同一服务,其中一台主机为工作机(PrimaryServer),另一台主机为备份机(StandbyServer),保证系统不间断的运行。双机热备软件就是实现上述功能的软件产品。双机热备针对的是服务器的临时故障所做的一种备份技术,通过双机热备,来避免长时间的服务中断,保证系统长期、可靠的服务。  企事业机构的信息化建设已…

    2022年4月6日
    42
  • 宏基因组数据分析:差异分析(LEfSe安装使用及LDA score计算)

    宏基因组数据分析:差异分析(LEfSe安装使用及LDA score计算)文章目录简介安装简介安装报错:Collectingpackagemetadata(current_repodata.json):doneSolvingenvironment:failedwithinitialfrozensolve.Retryingwithflexiblesolve.Solvingenvironment:failedwithrepodatafromcurrent_repodata.json,willretrywithnextre

    2022年6月5日
    98
  • 心形函数的几种表达式怎么求_心形曲线函数4种表达式

    心形函数的几种表达式怎么求_心形曲线函数4种表达式用两个函数表示:f(x)=sqrt(1-(abs(x)-1)^2)h(x)=-2*sqrt(1-0.5*abs(x))也可以根据图中的q(x)画出心形的内部:q(x)=(f(x)-h(x))/2*cos(200*x)+(f(x)+h(x))/2用一个函数表示,我拟合了很久才画出来的:f(x)=(0.64*sqrt(abs(x))-0.8+1.2^abs(x)*cos(200*x))*sqrt(cos(x))定义域:-pi/2<=x<=pi/2我个人觉得要比

    2022年10月9日
    0
  • huffman编码——原理与实现

    huffman编码——原理与实现

    2021年12月6日
    36
  • 常用的数据链路层协议_数据链路层和网络层

    常用的数据链路层协议_数据链路层和网络层由于以太网中的所有的主机共享一个通信信道,因此在同一时刻只允许有一台主机发送数据,否则各个主机发送的数据就会相互干扰。站在系统的角度来看,这里各个主机所共享的通信信道就是一种临界资源,这个临界资源同一时刻只允许一台主机使用。……

    2022年10月22日
    0
  • 使用Taro小程序框架开发一个学习、刷题、论坛、聊天交流的微信小程序

    使用Taro小程序框架开发一个学习、刷题、论坛、聊天交流的微信小程序项目介绍当代大学生上课缺少积极性,学习缺乏效率。同为大学生的我深有体会。所以特别开发出这样一款学习类的微信小程序帮助学生进行学习、巩固知识,同时增加对战PK模块来加强学生们的学习积极性。这是一个为学生提供在线学习课程、题库练习、考试答题、做题PK、上课签到、资料查阅、成绩分析等功能的微信小程序希望大佬们走过路过给个star~技术选型前端:Taro+微信小程序+Echarts后端:…

    2022年9月28日
    0

发表回复

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

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