编程语言
首页 > 编程语言> > java-MainActivity的AbstractAccountAuthenticator实现未调用addAccount

java-MainActivity的AbstractAccountAuthenticator实现未调用addAccount

作者:互联网

我正在使用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.
任何帮助将不胜感激.

解决方法:

好的,所以我终于确定了.显然,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参数传递. ew,这花了我一段时间,希望对别人有所帮助:-).

标签:accountmanager,java,android
来源: https://codeday.me/bug/20191028/1951711.html