I am developing an android app, using android studio (0.8.2) and API level 19.
I have implemented authentication using AccountManager, but am running into an exception when attempting to retrieve the data from the AccountManagerFuture returned by the callback specified in AccountManager.addAccount. This behavior only occurs when addAccount is called from within my app, the onCreate of MainActivity to be precise. The error does not occur when the account is added through the android settings screen. Here is the code that calls addAccount:
mAccounts = mAccountManager.getAccountsByType(mAccountType);
if (mAccounts.length == 0) {
Log.d("MainActivity", "No accounts of type " + mAccountType);
mAccountManager.addAccount(
mAccountType,
AUTHTOKEN_TYPE_FULL_ACCESS,
null,
new Bundle(),
this,
new OnAccountAddComplete(),
null
);
}
And here is the callback:
private class OnAccountAddComplete implements AccountManagerCallback<Bundle> {
@Override
public void run(AccountManagerFuture<Bundle> result) {
Bundle bundle;
try {
bundle = result.getResult();
} catch (OperationCanceledException e) {
e.printStackTrace();
return;
} catch (AuthenticatorException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
mAccount = new Account(
bundle.getString(AccountManager.KEY_ACCOUNT_NAME),
bundle.getString(AccountManager.KEY_ACCOUNT_TYPE)
);
// do more stuff
}
}
These follow the pattern shown in this example. The authentication system and activities closely follow the example given here, in particular the Log In and Sign Up activities. The exception is logged between the end of the SignUpActivity(line 104) and the callback in the AuthenticatorActivity (line 81)
W/System.err﹕ android.accounts.OperationCanceledException
W/System.err﹕ at android.accounts.AccountManager$AmsTask.internalGetResult(AccountManager.java:1503)
W/System.err﹕ at android.accounts.AccountManager$AmsTask.getResult(AccountManager.java:1531)
W/System.err﹕ at android.accounts.AccountManager$AmsTask.getResult(AccountManager.java:1452)
W/System.err﹕ at com.example.project.app.MainActivity$OnAccountAddComplete.run(MainActivity.java:256)
W/System.err﹕ at android.accounts.AccountManager$11.run(AccountManager.java:1427)
W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
The call to result.getResult
throws a android.accounts.OperationCanceledException
. The documentation only indicates that a user intervention or other event has cancelled the operation. Despite this, the new account is created and added to the AccountManager, and can be successfully used by the app once reopened.
This has been reproduced on different devices and workstations. Information on anything that would trigger the cancellation is also appreciated.