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()将会中断同步操作。

时间: 2024-08-04 22:20:19

android账号与同步之同步实现的相关文章

android账号与同步之发起同步

上一篇博文我介绍了账号与同步的同步实现过程,其中提供了一个工系统进程调用的服务,那么这个服务到底是怎么被启动和使用的呢?这篇博文我就大体梳理一下启动过程. 其实作为一个一般开发人员,我们只要知道要想知道被监听的ContentProvider有变动,首先那个ContentProvider必须使用ContentResolver.notifyChange(android.net.Uri, android.database.ContentObserver, boolean)这个方法来通知我们.我们知道这

android账号与同步之账号管理

在android提供的sdk中,samples目录下有一个叫SampleSyncAdapter的示例,它是一个账号与同步的实例,比如Google原始的android手机可以使用Google账号进行数据的同步.具体 的比如你想实时同步你的通讯录到服务端,这时候你就可以通过这个实例来了解android提供的同步机制,从而实现自己的同步功能. 本片博文先介绍一下账号的管理部分.至于账号管理的代码主要是在authenticator包下的三个类里面,还有就是一个叫authenticator的xml文件.

Android Wear 用于发送与同步数据的数据类型与接口概述

Android Wear数据层的API,是google play service的一部分,为您的手持设备和可穿戴式应用程序的通信通道. Api包括一系列数据对象,可以让系统通过监听器和线路通知app数据层重要的事件. 以下是几种对象类型. Data Item 提供数据存储用于手机和wear的自动同步. Messages 消息API可以实现fire-and-forget模式,如在wear中控制手机播放器控制或者通过手机发送一个意图给wear,当建立连接或者发生传输错误的时候,都会提供消息.消息是一

Android账号管理机制

在android提供的sdk中,samples目录下有一个叫SampleSyncAdapter的示例,它是一个账号与同步的实例,比如Google原始的android手机可以使用Google账号进行数据的同步.具体的比如你想实时同步你的通讯录到服务端,这时候你就可以通过这个实例来了解android提供的同步机制,从而实现自己的同步功能.先介绍一下账号的管理部分.至于账号管理的代码主要是在authenticator包下的三个类里面,还有就是一个叫authenticator 的xml文件. Authe

(转)同步音频--同步音频到视频

ffmpeg文档6 (2008-08-27 18:44:22) 转载 标签: 杂谈 分类: 翻译文档 指导6:同步音频 同步音频 现在我们已经有了一个比较像样的播放器.所以让我们看一下还有哪些零碎的东西没处理.上次,我们掩饰了一点同步问题,也就是同步音频到视频而不是其它的同 步方式.我们将采用和视频一样的方式:做一个内部视频时钟来记录视频线程播放了多久,然后同步音频到上面去.后面我们也来看一下如何推而广之把音频和视频 都同步到外部时钟. 生成一个视频时钟 现在我们要生成一个类似于上次我们的声音时

Android中Gallery和ImageSwitcher同步自动(滚动)播放图片库

本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) 本文是建立在以下两篇bolg上的:      1.Android入门第十二篇之Gallery                  2.Android 控件之ImageSwitcher图片切换器 如果对Gallery和ImageSwitcher控件不是很熟悉的同学,建议先过去看看,本文并没有怎么讲述控

Android 服务绑定与数据同步

1 package com.example.metrox.l15; 2 3 import android.content.ComponentName; 4 import android.content.Intent; 5 import android.content.ServiceConnection; 6 import android.os.IBinder; 7 import android.provider.Settings; 8 import android.support.v7.app.

Android Studio里Gradle Sync同步慢问题的解决方法

Android studio更新到3.3.1,完成后,gradle sysnc一直同步,1个小时后还在转圈圈.同步慢的不能忍受. 如何解决,网上一搜,发现阿里云有镜像,只需要添加相应的url地址就可以下载.如图,在gradle scripts–>build gradle.打开该文件. buildscript { repositories { maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} google() jc

android WebView 和 HttpClient cookie同步

首先android5.0 实现了WebView与 框架的自动cookie同步,无需额外操作. 5.0一下版本需要手动同步cookie 方法如下 <pre name="code" class="java">CookieSyncManager.createInstance(context); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCo