【问题标题】:add Account(settings) method doesn't start Activity添加帐户(设置)方法不启动活动
【发布时间】:2015-01-01 12:05:23
【问题描述】:

我想在设置中创建自定义应用帐户。

问题

  1. settings > Add account 中有一个带有图标但没有名称的选项
  2. 当点击那个(添加帐户)时,AuthenticatorActivity 不会启动。我调试了Authenticator 类,调用了addAccount 方法但没有弹出任何活动。

我做了以下步骤:

验证器类(部分)

public class AccountAuthenticator extends AbstractAccountAuthenticator{
    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response,
            String accountType, String authTokenType,
            String[] requiredFeatures, Bundle options)
            throws NetworkErrorException {
        final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
        intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_TYPE, accountType);
        intent.putExtra(AuthenticatorActivity.ARG_AUTH_TYPE, authTokenType);
        intent.putExtra(AuthenticatorActivity.ARG_IS_ADDING_NEW_ACCOUNT, true);
        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
        final Bundle bundle = new Bundle();
        bundle.putParcelable(AccountAuthenticator.KEY_INTENT, intent);
        return bundle;
    }
}

AuthenticatorService

public class AuthenticatorService extends Service{
     @Override
    public IBinder onBind(Intent intent) {
        authenticator = new AccountAuthenticator(this);
        return authenticator.getIBinder();
    }
}

清单

<service android:name="com.voillo.utils.AuthenticatorService" android:exported="false"
            android:label="@string/app_name">
     <intent-filter>
        <action android:name="android.accounts.AccountAuthenticator" />
     </intent-filter>
     <meta-data android:name="android.accounts.AccountAuthenticator"
               android:resource="@xml/authenticator" />
</service>

身份验证器 xml

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
     android:accountType="com.example.myapp"
     android:icon="@drawable/myapp_icon"
     android:smallIcon="@drawable/myapp_icon_small"
     android:label="myapp"
     />

【问题讨论】:

  • 我刚刚遇到了类似的问题,结果证明我没有在清单 (facepalm) 中添加身份验证器活动作为活动。确保这不是您的问题。

标签: android account


【解决方案1】:

您很可能忘记在清单文件中声明活动。我遇到了同样的错误,发现我忘记在项目的 Manifest 上声明我的活动。

【讨论】:

    【解决方案2】:

    我为此奋斗了一段时间,遇到了同样的问题:我的 AddAccount() 被电话设置调用,但 Activity 没有启动。我设法通过将附加功能放在作为方法参数传递的包中而不是创建新包来修复它。在您的情况下,它应该看起来像这样:

    public class AccountAuthenticator extends AbstractAccountAuthenticator{
    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response,
            String accountType, String authTokenType,
            String[] requiredFeatures, Bundle options)
            throws NetworkErrorException {
        final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
        intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_TYPE, accountType);
        intent.putExtra(AuthenticatorActivity.ARG_AUTH_TYPE, authTokenType);
        intent.putExtra(AuthenticatorActivity.ARG_IS_ADDING_NEW_ACCOUNT, true);
        intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    
        bundle.putParcelable(AccountAuthenticator.KEY_INTENT, intent);
        return bundle;
    }
    

    还有关于 XML 文件中的标签,您需要使用资源使其显示在电话设置中。

        android:label="@string/app_name"
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-04
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      相关资源
      最近更新 更多