【问题标题】:addAccount not called from AbstractAccountAuthenticator implementation from MainActivity未从 MainActivity 的 AbstractAccountAuthenticator 实现调用 addAccount
【发布时间】:2015-05-12 16:12:39
【问题描述】:

我正在关注 tutorial 向 Android AccountManager 添加用户帐户。

在我的主要活动中,我有以下方法:

private void addNewAccount(String accountType, String authTokenType) {
    Log.d(TAG,"addNewAccount called");
    final AccountManagerFuture<Bundle> future = mAccountManager.addAccount(accountType, authTokenType, null, null, this, new AccountManagerCallback<Bundle>() {
        @Override
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                Bundle bnd = future.getResult();
                Log.d("ACME", "AddNewAccount Bundle is " + bnd);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, null);
}

当我在 logcat 中看到日志时,正在调用此方法。现在我的 AbstractAccountAuthenticator 实现如下:

public class AcmeAuthenticator extends AbstractAccountAuthenticator {

private String TAG = "AcmeAuthenticator";
private final Context mContext;

public AcmeAuthenticator(Context context) {
    super(context);
    this.mContext = context;
}

@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
    Log.d("acme", TAG + "> addAccount");

    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(AccountManager.KEY_INTENT, intent);
    return bundle;
}

上述方法永远不会被调用。以下是我为其创建的服务:

public class AcmeAuthenticatorService extends Service {
@Override
public IBinder onBind(Intent intent) {

    AcmeAuthenticator authenticator = new AcmeAuthenticator(this);
    return authenticator.getIBinder();
}
}

而我的清单定义如下:

<activity android:name="com.exercise.accountmanagerstudy.accountAuthenticator.AuthenticatorActivity" android:label="@string/login_label"/>
    <service android:name=".accountAuthenticator.AcmeAuthenticatorService">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator" />
        </intent-filter>
        <meta-data android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>
<!-- client -->
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>

<!-- Authenticator -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>

我没有收到编译器错误,未调用 AbstractAccountAuthenticator 实现中的 addAccount 覆盖。从主要活动 addNewAccount 方法。我已经研究了几个链接herehere。 任何帮助将不胜感激。

【问题讨论】:

  • "addAccount not called from AbstractAccountAuthenticator..." - 如果它是抽象的,那么就没有代码,所以它什么都不能。
  • 我创建了一个名为 AcmeAuthenticator 的 AbstractAccountAuthenticator 实现(第二个代码块)。它不会从实现或扩展 AbstractAccountAuthenticator 的类中调用。

标签: java android accountmanager


【解决方案1】:

好吧,我终于弄明白了。显然,AcmeAuthenticator 的authenticator.xml 文件有一个名为accountType 的字段:

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.exercise.accountmanagerstudy"
android:icon="@drawable/ic_launcher"
android:smallIcon="@drawable/ic_launcher"
android:label="@string/label"
android:accountPreferences="@xml/prefs"/>

当我在我的主要活动中调用 addNewAccount 时,我应该将上述 xml 中 accountType 的确切值作为 accountType 参数传递。唷,这花了我相当长的时间,希望它可以帮助别人:-)。

【讨论】:

  • 遇到了类似的问题。我认为 AccountManager.KEY_ACCOUNT_TYPE 会选择在authenticator.xml 文件中设置的值,而无需我自己传递它。
猜你喜欢
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
相关资源
最近更新 更多