标签:accountmanager authentication android
我试图编写自己的身份验证器并将其用作具有两个不同应用程序的库:A和B.
我关注了这个帖子:http://udinic.wordpress.com/2013/04/24/write-your-own-android-authenticator/.
我先安装了应用程序A,然后安装了应用程序B.当应用程序A调用AccountManager.addAccount()时,将打开AccountAuthenticatorActivity.当应用B调用AccountManager.addAccount()时,什么都没有发生.如果我卸载应用程序A并在应用程序B中重试,则AccountAuthenticatorActivity将打开.
我的目标是为两个应用程序使用相同的AccountAuthenticatorActivity和AccountAuthenticator,但它似乎一次只能在一个应用程序上工作.
这是我的AbstractAccountAuthenticator中的addAccount:
@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(AccountManager.KEY_INTENT, intent);
return bundle;
}
这是我从两个应用程序中调用accountManager.addAccount()的方式:
private void addNewAccount(String accountType, String authTokenType) {
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();
} catch (Exception e) {
e.printStackTrace();
}
}
}, null);
}
解决方法:
我遇到了同样的问题,所有这些混乱的关键是AndroidManifest.xml
关于如何创建自定义身份验证器的教程有些过时(或至少在Android Studio之前),因此它们指出您必须将权限,活动和服务从身份验证器库复制到将要使用此库的所有应用程序中错了Android Studio,因为Android Studio会自动合并所有模块的清单.
如果您使用的是Android Studio,则只需在身份验证器模块中允许权限,活动和服务,然后在应用程序中向该模块添加依赖项即可.
这是一个示例AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:label="@string/auth_app_name"
android:theme="@style/Holo.Theme.Light.DarkActionBar">
<activity
android:name="ro.muerte.modules.auth.MyAuthenticatorActivity"
android:exported="true"
android:label="@string/auth_action_login"
android:windowSoftInputMode="adjustResize|stateVisible"
android:clearTaskOnLaunch="true" />
<service
android:name="ro.muerte.modules.auth.MyAuthenticateService"
android:exported="true">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
</application>
标签:accountmanager,authentication,android 来源: https://codeday.me/bug/20191030/1966767.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。