如何构建Android Sync Provider :Part1

简介:

 

Android2.0 SDK带来的一个好东西就是你可以写一个普通的同步供应商程序,并将其与系统的联系薄,日历等集成。唯一的问题是相关的文档十分的少。还有一个糟糕的问题在于,如果你在某一个地方出了错,Android系统就会崩溃重启。面临如此挑战,我依靠这稀少的文档,以及很少的帖子,还有Android地带的一些代码去建立了一个同步程序----Last.fm 。你想要知道怎么去建立你自己的同步程序么?读下去吧

账户验证

第一个令人疑惑的问题就是账户验证问题,你可以从这个地方了解到更多的信息。http://developer.android.com/reference/android/accounts/AbstractAccountAuthenticator.html

这里定义了该账户如何在“账号&同步”设置中出现的。一个账号的的验证需要3部分来实现:1. 一个从onBind方法返回AbstractAccountAuthenticator 子类的一个服务2. 一个Activityt提供用户输入他们的凭据(账号秘密信息),一个xml文件去描述账号信息展示给用户时( an xml file describing how your account should look when displayed to the user.)同时,你也需要在android.mainfest.xml中添加android.permission.AUTHENTICATE_ACCOUNTS权限

服务

验证服务程序期望从一个onBind方法中返回一个AbstractAccountAuthenticator 的子类。如果你坚持不这么做的话,带来的后果就是当你向系统添加一个账号时,android会将会崩溃并且重启。所幸实现AbstractAccountAuthenticator 并不是一件困难的事情,我们只需要实现其中的addAccount方法即可。该方法返回一个Intent,系统将会用他来为用户展示一个登陆框。该如下的实现将会运行我们的service“fm.last.android.sync.LOGIN”.用户等登录完毕后,,会有一个AccountAuthenticatorResponse 对象传出,我们可以用来将其回传给系统。

AccountAuthenticatorService.java

1   import   fm . last . android . LastFm; 
2   import   android . accounts . AbstractAccountAuthenticator; 
3   import   android . accounts . Account; 
4   import   android . accounts . AccountAuthenticatorResponse; 
5   import   android . accounts . AccountManager; 
6   import   android . accounts . NetworkErrorException; 
7   import   android . app . Service; 
8   import   android . content . Context; 
9   import   android . content . Intent; 
10  import   android . os . Bundle; 
11  import   android . os . IBinder; 
12  import   android . util . Log; 
13    
14  /* * 
15    *   Authenticator   service   that   returns   a   subclass   of   AbstractAccountAuthenticator   in   onBind() 
16    */ 
17  public   class   AccountAuthenticatorService   extends   Service   { 
18    private   static   final   String   TAG   =   " AccountAuthenticatorService " ; 
19    private   static   AccountAuthenticatorImpl   sAccountAuthenticator   =   null ; 
20    
21    public   AccountAuthenticatorService()   { 
22      super (); 
23    } 
24    
25    public   IBinder   onBind(Intent   intent)   { 
26      IBinder   ret   =   null ; 
27      if   (intent . getAction() . equals(android . accounts . AccountManager . ACTION_AUTHENTICATOR_INTENT)) 
28        ret   =   getAuthenticator() . getIBinder(); 
29      return   ret; 
30    } 
31    
32    private   AccountAuthenticatorImpl   getAuthenticator()   { 
33      if   (sAccountAuthenticator   = =   null ) 
34        sAccountAuthenticator   =   new   AccountAuthenticatorImpl( this ); 
35      return   sAccountAuthenticator; 
36    } 
37    
38    private   static   class   AccountAuthenticatorImpl   extends   AbstractAccountAuthenticator   { 
39      private   Context   mContext; 
40    
41      public   AccountAuthenticatorImpl(Context   context)   { 
42        super (context); 
43        mContext   =   context; 
44      } 
45    
46      /* 
47        *     The   user   has   requested   to   add   a   new   account   to   the   system.     We   return   an   intent   that   will   launch   our   login   screen   if   the   user   has   not   logged   in   yet, 
48        *     otherwise   our   activity   will   just   pass   the   user's   credentials   on   to   the   account   manager. 
49        */ 
50      @Override 
51      public   Bundle   addAccount(AccountAuthenticatorResponse   response,   String   accountType,   String   authTokenType,   String[]   requiredFeatures,   Bundle   options) 
52          throws   NetworkErrorException   { 
53        Bundle   reply   =   new   Bundle(); 
54        
55        Intent   i   =   new   Intent(mContext,   LastFm . class ); 
56        i . setAction( " fm.last.android.sync.LOGIN " ); 
57        i . putExtra(AccountManager . KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,   response); 
58        reply . putParcelable(AccountManager . KEY_INTENT,   i); 
59        
60        return   reply; 
61      } 
62    
63      @Override 
64      public   Bundle   confirmCredentials(AccountAuthenticatorResponse   response,   Account   account,   Bundle   options)   {
65        return   null ; 
66      } 
67    
68      @Override 
69      public   Bundle   editProperties(AccountAuthenticatorResponse   response,   String   accountType)   { 
70        return   null ; 
71      } 
72    
73      @Override 
74      public   Bundle   getAuthToken(AccountAuthenticatorResponse   response,   Account   account,   String   authTokenType,   Bundle   options)   throws   NetworkErrorException   { 
75        return   null ; 
76      } 
77    
78      @Override 
79      public   String   getAuthTokenLabel(String   authTokenType)   { 
80        return   null ; 
81      } 
82    
83      @Override 
84      public   Bundle   hasFeatures(AccountAuthenticatorResponse   response,   Account   account,   String[]   features)   throws   NetworkErrorException   { 
85        return   null ; 
86      }   
87      @Override 
88      public   Bundle   updateCredentials(AccountAuthenticatorResponse   response,   Account   account,   String   authTokenType,   Bundle   options)   { 
89        return   null ; 
90      } 
91    } 
92  } 
93

该验证服务需要在Android.Mainfest.xml中使用元数据标签定义一下。如下

Snippet from AndroidManifest.xml

< service  android:name = " AccountAuthenticatorService " 
android:exported = " true "  android:process = " :auth " > 
  < intent-filter > 
    < action  android:name = " android.accounts.AccountAuthenticator "   / > 
  < /intent-filter > 
  < meta-data  android:name = " android.accounts.AccountAuthenticator " 
   android:resource = " @xml/authenticator "   / > 
< /service > 
9

Xml文件

账号的xml文件中定义了当应用程序与你的账号互动的时候,应用程序将会看到的东西(是不是向国内其他应用程序使用QQ一些应用一样,首先会有一个授权,询问用户当前的应用程序可以访问你的用户的哪些信息?------我的理解)其中包含了用户可读的名字,你所定义的系统账号类型,图标,对一个包含当用户修改账号时可以看到的PreferenceScreens 的xml文件。

authenticator.xml

< account-authenticator  xmlns:android = " http://schemas.android.com/apk/res/android " 
       android:accountType = " fm.last.android.account " 
       android:icon = " @drawable/icon " 
       android:smallIcon = " @drawable/icon " 
       android:label = " @string/app_name " 
       android:accountPreferences = " @xml/account_preferences " / > 
7

account_preferences.xml

1   < PreferenceScreen 
2      xmlns:android = " http://schemas.android.com/apk/res/android " > 
3           < PreferenceCategory 
4                          android:title = " General   Settings "   / > 
5     
6           < PreferenceScreen 
7                  android:key = " account_settings " 
8                  android:title = " Account   Settings " 
9                  android:summary = " Sync   frequency,   notifications,   etc. " > 
10                  < intent 
11                         android:action = " fm.last.android.activity.Preferences.ACCOUNT_SETUP " 
12                         android:targetPackage = " fm.last.android " 
13                         android:targetClass = " fm.last.android.activity.Preferences "   / > 
14          < /PreferenceScreen > 
15  < /PreferenceScreen > 
16

集成(putting it all together)

现在我们可以准备开始测试了。Android 账号的设置部分并不能完好的捕捉异常。如果有地方出错了,设备会重启。最好的测试方式是运行模拟器后,运行DevTools,点击AcountsTester

clip_image001

你会看到一个新的账号类型将会同系统内置的“Corporate”类型的账号一样,被添加到列表中。尽管从下拉列表中选择你的账号,然后点击增加按钮,那么,将会呈现一个你所做的登录框。经过验证后 ,你的账号将会出现在按钮下边的列表中。在这一点,使用系统的“Account&sync”设置去移除和修改账户应该是安全的。

clip_image002 clip_image003

准备好“Data & synchronization”的章节了么?让我们开始第二个章节吧。

可供参考的实现源代码你可以从这里下载https://github.com/c99koder/lastfm-android/。(在GNUGeneralPublicLicense主题下边)另外一个单独的实例程序你可以从Apache License 2.0主题下得到https://github.com/c99koder/AndroidSyncProviderDemo。Google同样也有一个他们的示例的同步程序在Androiddeveloperportal http://developer.android.com/resources/samples/SampleSyncAdapter/index.html。该应用程序比我(原作者)的会稍微完整一些。



本文转自HDDevTeam 51CTO博客,原文链接:http://blog.51cto.com/hddev/653349,如需转载请自行联系原作者

相关文章
|
2月前
|
存储 安全 Android开发
构建高效的Android应用:Kotlin与Jetpack的结合
【5月更文挑战第31天】 在移动开发的世界中,Android 平台因其开放性和广泛的用户基础而备受开发者青睐。随着技术的进步和用户需求的不断升级,开发一个高效、流畅且易于维护的 Android 应用变得愈发重要。本文将探讨如何通过结合现代编程语言 Kotlin 和 Android Jetpack 组件来提升 Android 应用的性能和可维护性。我们将深入分析 Kotlin 语言的优势,探索 Jetpack 组件的核心功能,并通过实例演示如何在实际项目中应用这些技术。
|
9天前
|
安全 Java Android开发
探索安卓与iOS开发的差异:构建未来应用的关键考量
【7月更文挑战第18天】在移动应用开发的广阔天地中,安卓和iOS两大平台各领风骚。本文将深入探讨这两个平台在开发过程中的主要差异,包括编程语言、用户界面设计、性能优化、安全性以及市场策略等方面。通过比较分析,旨在为开发者提供决策支持,帮助他们选择最适合自己项目需求的平台,同时考虑到用户体验和市场需求的变化,为未来的应用开发指明方向。
|
29天前
|
Java Maven Android开发
安卓项目使用阿里云镜像加速构建过程
安卓项目使用阿里云镜像加速构建过程
22 0
|
1月前
|
安全 数据库 Android开发
45. 【Android教程】内容提供者 - Content Provider
45. 【Android教程】内容提供者 - Content Provider
21 2
|
2月前
|
安全 物联网 测试技术
构建未来:Android与IoT设备的无缝交互深入探索软件自动化测试的未来趋势
【5月更文挑战第30天】在物联网(IoT)技术快速发展的当下,Android系统因其开放性和广泛的用户基础成为了连接智能设备的首选平台。本文将探讨如何通过现代Android开发技术实现智能手机与IoT设备的高效、稳定连接,并分析其中的挑战和解决方案。我们将深入挖掘Android系统的底层通信机制,提出创新的交互模式,并通过实例演示如何在Android应用中集成IoT控制功能,旨在为开发者提供一套可行的指导方案,促进IoT生态系统的进一步发展。
|
2月前
|
JSON Android开发 开发者
构建高效Android应用:采用Kotlin协程优化网络请求
【5月更文挑战第31天】 在移动开发领域,尤其是针对Android平台,网络请求的管理和性能优化一直是开发者关注的焦点。随着Kotlin语言的普及,其提供的协程特性为异步编程提供了全新的解决方案。本文将深入探讨如何利用Kotlin协程来优化Android应用中的网络请求,从而提升应用的响应速度和用户体验。我们将通过具体实例分析协程与传统异步处理方式的差异,并展示如何在现有项目中集成协程进行网络请求优化。
|
1月前
|
机器学习/深度学习 搜索推荐 Android开发
在安卓应用开发中,构建高效的用户界面是至关重要的一环
【6月更文挑战第10天】本文是关于构建高效安卓用户界面的指南,分为设计原则和技巧两部分。设计原则包括一致性、简洁性和可访问性,强调遵循安卓系统规范、保持界面简洁及考虑不同用户需求。技巧方面,建议合理布局、优化图标和图片、使用动画效果、提供个性化设置以及优化性能。随着技术发展,开发者需关注AI和机器学习,以创新应用体验,适应用户需求变化。
35 0
|
1月前
|
编解码 缓存 Android开发
构建高效的Android应用:从内存优化到响应式设计
【5月更文挑战第37天】 在竞争激烈的移动应用市场中,一个高效、流畅的Android应用是吸引和保留用户的关键。本文将深入探讨构建高效Android应用的多个关键方面,包括内存优化策略、布局性能和响应式设计原则。我们将通过具体的技术实践和案例分析,揭示如何提升应用性能,减少资源消耗,并确保在不同设备上的兼容性和用户体验一致性。
|
2月前
|
Java Android开发 开发者
构建高效Android应用:Kotlin协程的实践指南
【5月更文挑战第31天】在现代Android开发中,异步编程和性能优化成为关键要素。Kotlin协程作为一种在JVM上实现轻量级线程的方式,为开发者提供了简洁而强大的并发处理工具。本文深入探讨了如何在Android项目中利用Kotlin协程提升应用的响应性和效率,包括协程的基本概念、结构以及实际运用场景,旨在帮助开发者通过具体实例理解并掌握协程技术,从而构建更加流畅和高效的Android应用。
|
2月前
|
数据库 Android开发 开发者
构建高效Android应用:Kotlin协程的全面指南
【5月更文挑战第31天】 在移动开发领域,性能优化和流畅的用户体验是至关重要的。随着Kotlin语言在Android开发中的普及,其提供的协程功能已成为简化异步编程、提高应用响应性和效率的强大工具。本文将深入探讨Kotlin协程的概念、优势以及如何在Android应用中实现它们。通过实际案例分析,我们将展示如何利用协程提升数据处理能力,同时保持UI线程不被阻塞,确保用户界面流畅无阻。