我想在android上添加AccountManager.addAccount()的自定义帐户.我正在关注this tutorial.当我尝试使用AccountManagerCallback的run方法获取结果时,我收到带有消息的AuthenticatorException:androID.accounts.AuthenticatorException:bind failure.
经过一些研究后,我发现了两个可能的解决方案,但我已经在应用程序标签内部declared authenticator和checked my account type.我还将清单权限与教程中的权限进行了比较.我正在使用androID studio 1.4,我在几个仿真器和物理设备上尝试过它.
这是我的AndroIDManifest.xml,还有authenticator.xml和account_preferences.xml:
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.test.myproject" > <uses-permission androID:name="androID.permission.INTERNET" /> <uses-permission androID:name="androID.permission.USE_CREDENTIALS" /> <uses-permission androID:name="androID.permission.GET_ACCOUNTS" /> <uses-permission androID:name="androID.permission.READ_PROfile" /> <uses-permission androID:name="androID.permission.READ_CONTACTS" /> <uses-permission androID:name="androID.permission.AUTHENTICATE_ACCOUNTS"/> <uses-permission androID:name="androID.permission.MANAGE_ACCOUNTS"/> <application androID:allowBackup="true" androID:icon="@mipmap/test_logo" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name=".vIEw.MainActivity" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name=".vIEw.LoginActivity" androID:label="@string/app_name"> </activity> <service androID:name="com.test.myproject.model.utility.MyAuthenticatorService"> <intent-filter> <action androID:name="androID.accounts.AccountAuthenticator"/> </intent-filter> <Meta-data androID:name="androID.accounts.AccountAuthenticator" androID:resource="@xml/authenticator" /> </service> <Meta-data androID:name="com.Google.androID.gms.version" androID:value="@integer/Google_play_services_version" /> </application></manifest>
我也试过命名服务.model.utility.MyAuthenticatorService没有任何效果.
authenticator.xml:
<?xml version="1.0" enCoding="utf-8"?><PreferenceScreen xmlns:androID="http://schemas.androID.com/apk/res/androID"> <account-authenticator xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:accountType="com.test.myproject" androID:icon="@drawable/test_logo" androID:smallicon="@drawable/test_logo" androID:label="@string/not_implemented" androID:accountPreferences="@xml/account_preferences" /></PreferenceScreen>
account_preferences.xml:
<?xml version="1.0" enCoding="utf-8"?><PreferenceScreen xmlns:androID="http://schemas.androID.com/apk/res/androID"> <Preferencecategory androID:title="@string/not_implemented" /> <CheckBoxPreference androID:title="Use deBUG server" androID:key="isDeBUG" androID:summary="Connecting to a deBUG server instead of prod server"/> <SwitchPreference androID:title="DeBUG Logs" androID:key="logsverbose" androID:summary="Show deBUG logs on LogCat"/></PreferenceScreen>
这是MyAuthenticatorService:
public class MyAuthenticatorService extends Service { @Nullable @OverrIDe public IBinder onBind(Intent intent) { MyAuthenticator myAuthenticator = new MyAuthenticator(this); return myAuthenticator.getIBinder(); }}
这是MyAuthenticator:
public class MyAuthenticator extends AbstractAccountAuthenticator { private Context context; public MyAuthenticator(Context context) { super(context); this.context = context; } @OverrIDe public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException { final Intent intent = new Intent(context, LoginActivity.class); intent.putExtra(LoginActivity.ARG_ACCOUNT_TYPE, accountType); intent.putExtra(LoginActivity.ARG_AUTH_TYPE, authTokenType); intent.putExtra(LoginActivity.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; } //other overrIDe methods (they all return null for Now)}
这是MainActivity:
public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private NavigationVIEw navigationVIEw; private DrawerLayout drawerLayout; private SharedPreferences sharedPreferences; private AccountManager accountManager; private static final String ACCOUNT_TYPE = "com.test.myproject"; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_drawer); this.accountManager = AccountManager.get(this); signIn(ACCOUNT_TYPE, "access_token"); //other stuff } private voID signIn(String accountType, String authTokenType) { final AccountManagerFuture<Bundle> future = accountManager.addAccount(ACCOUNT_TYPE, authTokenType, null, null, this, new AccountManagerCallback<Bundle>() { @OverrIDe public voID run(AccountManagerFuture<Bundle> future) { try { Bundle bnd = future.getResult(); showMessage("Account was created"); Log.d("udinic", "AddNewAccount Bundle is " + bnd); } catch (Exception e) { e.printstacktrace(); showMessage(e.getMessage()); } } }, null); }}
当debBUGing时,在Bundle bnd = future.getResult();上抛出错误,并且future具有状态3和结果androID.accounts.AuthenticatorException:绑定失败.艰难的执行永远不会到达LoginActivity,或者至少不会触发断点,这里是:
public class LoginActivity extends AccountAuthenticatorActivity { public final static String ARG_ACCOUNT_TYPE = "ACCOUNT_TYPE"; public final static String ARG_ACCOUNT_name = "AUTH_TYPE"; public final static String ARG_AUTH_TYPE = "ACCOUNT_name"; public final static String ParaM_USER_PASS = "USER_PASS"; private AccountManager accountManager; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_login); this.accountManager = AccountManager.get(getBaseContext()); } public voID login(VIEw vIEw) { EditText usernameEditText = (EditText)findVIEwByID(R.ID.textLoginUsername); EditText passwordEditText = (EditText)findVIEwByID(R.ID.textLoginPassword); String ownerUsername = usernameEditText.getText().toString(); String ownerPassword = passwordEditText.getText().toString(); String clIEntID = "test"; String clIEntSecret = "test"; AccountManager manager = AccountManager.get(this); TokenRequestTask tokenRequestTask = new TokenRequestTask(); tokenRequestTask.execute(ownerUsername, ownerPassword, clIEntID, clIEntSecret); } private class TokenRequestTask extends AsyncTask<String, VoID, Intent> { @OverrIDe protected Intent doInBackground(String... params) { final String accountType = getIntent().getStringExtra(ARG_ACCOUNT_TYPE); String ownerUsername = params[0]; String ownerSecret = params[1]; String clIEntID = params[2]; String clIEntSecret = params[3]; String authToken = signIn(clIEntID, clIEntSecret, ownerUsername, ownerSecret, "password"); Bundle resultData = new Bundle(); resultData.putString(AccountManager.KEY_ACCOUNT_name, ownerUsername); resultData.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); resultData.putString(AccountManager.KEY_AUTHTOKEN, authToken); resultData.putString(ParaM_USER_PASS, ownerSecret); final Intent resultIntent = new Intent(); resultIntent.putExtras(resultData); return resultIntent; } } private String signIn(String clIEntID, String clIEntSecret, String ownerUsername, String ownerSecret, String grantType) { MyAPI20ServiceImpl service = (MyAPI20ServiceImpl)new ServiceBuilder().provIDer(MyAPI20.class) .APIKey(clIEntID) .APISecret(clIEntSecret) .signatureType(SignatureType.queryString) .build(); Token token = service.getAccesstoken(ownerUsername, ownerSecret, grantType); return token.getToken(); }}
这是完整的堆栈跟踪:
10-19 10:39:05.042 25931-25931/? I/art: Not late-enabling -Xcheck:jni (already on)10-19 10:39:05.042 25931-25931/? I/art: Late-enabling JIT10-19 10:39:05.068 25931-25931/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=100010-19 10:39:05.118 25931-25931/com.test.myproject W/System: ClassLoader referenced unkNown path: /data/app/com.test.myproject-1/lib/x8610-19 10:39:05.355 25931-25960/com.test.myproject D/Openglrenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true10-19 10:39:05.358 25931-25931/com.test.myproject D/: HostConnection::get() New Host Connection established 0xad974e50, tID 2593110-19 10:39:05.366 25931-25931/com.test.myproject W/System.err: androID.accounts.AuthenticatorException: bind failure10-19 10:39:05.366 25931-25931/com.test.myproject W/System.err: at androID.accounts.AccountManager.convertErrorToException(AccountManager.java:2147)10-19 10:39:05.366 25931-25931/com.test.myproject W/System.err: at androID.accounts.AccountManager.-wrap0(AccountManager.java)10-19 10:39:05.366 25931-25931/com.test.myproject W/System.err: at androID.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1990)10-19 10:39:05.366 25931-25931/com.test.myproject W/System.err: at androID.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)10-19 10:39:05.366 25931-25931/com.test.myproject W/System.err: at androID.os.Binder.execTransact(Binder.java:453)10-19 10:39:05.423 25931-25960/com.test.myproject D/: HostConnection::get() New Host Connection established 0xac17e080, tID 2596010-19 10:39:05.433 25931-25960/com.test.myproject I/Openglrenderer: Initialized EGL, version 1.410-19 10:39:05.538 25931-25960/com.test.myproject W/EGL_emulation: eglSurfaceAttrib not implemented10-19 10:39:05.538 25931-25960/com.test.myproject W/Openglrenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf125cc0, error=EGL_SUCCESS10-19 10:39:05.903 25931-25931/com.test.myproject I/Choreographer: Skipped 31 frames! The application may be doing too much work on its main thread.10-19 10:39:05.975 25931-25960/com.test.myproject W/EGL_emulation: eglSurfaceAttrib not implemented10-19 10:39:05.975 25931-25960/com.test.myproject W/Openglrenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3fe0c40, error=EGL_SUCCESS10-19 10:39:07.392 25931-25960/com.test.myproject E/Surface: getSlotFromBufferLocked: unkNown buffer: 0xac027c50
有人可以帮我解决这个问题吗?
解决方法:
好的,所以,它真的是一个复制粘贴错误,它让我发疯.在authenticator.xml中:
<?xml version="1.0" enCoding="utf-8"?><PreferenceScreen xmlns:androID="http://schemas.androID.com/apk/res/androID"> <account-authenticator xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:accountType="com.test.myproject" androID:icon="@drawable/test_logo" androID:smallicon="@drawable/test_logo" androID:label="@string/not_implemented" androID:accountPreferences="@xml/account_preferences" /></PreferenceScreen>
< / PreferenceScreen>不应该在这里.
如果您收到此错误,请检查您的xml文件.
总结以上是内存溢出为你收集整理的java – Android AccountManager抛出AuthenticatorException:添加帐户时绑定失败全部内容,希望文章能够帮你解决java – Android AccountManager抛出AuthenticatorException:添加帐户时绑定失败所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)