1

我连续十个小时尝试在我的应用程序上创建自定义帐户类型。

我的参考资料在这里:http: //docs.huihoo.com/android/4.2/training/id-auth/custom_auth.html

还有这里的 Android 开发者示例。

我的步骤是:

  1. 创建一个Authenticator扩展AbstractAccountAuthenticator并覆盖 7 个方法的类。
  2. 创建AuthenticatorService扩展Service我初始化类的类的Authenticator
  3. 创建AuthenticatorActivity哪些扩展AccountAuthenticatorActivity以及我创建用户界面以输入凭据的位置。
  4. 在中添加服务、权限和活动AndroidManifest.xml
  5. 创建@xml/authenticator文件并设置信息

所有这些都做得很好,唯一要做的就是找到一种方法来启动服务。所以我创建了另一个活动并在onCreate()方法中启动它。

是时候测试应用程序了,我决定在模拟器(API16)上运行它。启动了,MainActivity我们可以在 Running Processes 中看到应用程序进程和应用程序服务!

所以我去 DevTools/AccountsTester 插入凭据......但这里什么都没有。默认情况下只有企业和电子邮件应用程序。我以为是模拟器的原因,所以在我的GS3上尝试添加一个帐户,但它是一样的。

在我的 logcat 中没有错误,我们可以看到服务正在运行(使用Log我的服务类中的类)。

我的代码与我的第二个参考完全相同,我只是通过返回一个简单的true来更改服务器请求,因此在我的情况下,该服务不使用外部服务器来获取有关提交凭据的响应。

我的工作中有没有遗漏的步骤?

4

1 回答 1

1

我终于发现了这个小错误。当您使用 Android Studio 并创建一个新的 XML 资源文件(在示例中为 authenticationator.xml 文件)时,会自动生成一些代码:

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

</ressources>

我的错误是将我的account-authenticatior项目插入ressources,而不是让它作为根。

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="e@string/account_type"
    android:icon="@drawable/myIcon"
    android:smallIcon="@drawable/myIcon"
    android:label="@string/app_name"
    />

我让这个答案给像我这样可能会错过的人。我还删除了startService()my 中的方法MainActivity,因为它AuthenticatorService是自动启动的。

于 2014-08-21T07:50:25.283 回答