2011-10-05 89 views
3

我的应用程序需要同步服务器中的一些数据。我添加了必要的类(类似于SampleSyncAdapter),现在我可以通过“设置/同步和帐户”添加帐户。 但是我希望在安装应用程序后立即添加我的帐户和工作同步(我不希望用户在设置中进行任何手动更改)。这个怎么做?自动添加帐户

回答

7

有点晚了,但...

Account account = new Account("Title", "com.package.nom"); 
String password = "password"; 
AccountManager accountManager = AccountManager.get(context); 
accountManager.addAccountExplicitly(account, password, null); 
+0

不幸的是,它看起来并不那么简单(它会失败,因为java.lang.SecurityException:caller uid 10007与认证者的uid不同)...请参阅Udinic的答案为更完整的答案。 (也许我错过了什么,但我真的不明白为什么这个答案被接受和upvoted?) – ben75

+0

@ ben75显然是因为它在我发布它的时候工作。 –

+0

如果您按照伊恩的建议,请考虑以下内容:https://stuff.mit.edu/afs/sipb/project/android/docs/training/id-auth/custom_auth.html#Security – Cray

1

你没有添加到发布的埃文埃利奥特下面的代码:

Account account = new Account("Title", "com.package.nom"); 
String password = "password"; 
AccountManager accountManager = AccountManager.get(context); 
accountManager.addAccountExplicitly(account, password, null); 

以下:

authenticator.xml

<?xml version="1.0" encoding="utf-8"?> 

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:accountType="@string/account_type" 
    android:icon="@drawable/icon_hdpi" 
    android:smallIcon="@drawable/icon_hdpi" 
    android:label="@string/authenticator_label" 
/> 

和权限?

+0

你的意思是,我们需要添加这段代码,权限和这个XML文件,然后帐户将被添加。我正在尝试添加帐户,但我收到错误消息。我应该在哪里包含这个XML文件?我需要在代码中引用这个文件吗?你能解释一下吗? :( – blackfyre

+0

谷歌不会让你从代码中添加帐户,用户必须自己做,我放弃了试图使用这些方法并使用现有的方法 – Li3ro

+0

@ Li3ro,你是这么认为的?Whatsapp,Facebook, Linkedin等这样做了吗?我仍然在寻找答案btw .. :-) – Tom