无法从不同的应用程序获取自定义帐户的 AuthToken
Cannot get AuthToken for custom account from different app
我有两个使用相同帐户类型的应用程序。当用户第一次打开第二个应用程序并且存在一个帐户时,我希望显示以下页面:
但是当我运行这个代码时没有任何反应:
final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, authTokenType, null, this, null, null);
new Thread(new Runnable() {
@Override
public void run() {
try {
Bundle bnd = future.getResult();
final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
showMessage((authtoken != null) ? "SUCCESS!\ntoken: " + authtoken : "FAIL");
Log.d("udinic", "GetToken Bundle is " + bnd);
} catch (Exception e) {
e.printStackTrace();
showMessage(e.getMessage());
}
}
}).start();
当我从具有身份验证器的应用程序 运行 上面的代码时,它可以正常工作。当我 运行 下面的代码改为时,系统会生成一个通知,当我点击它时,会出现上面的图片。
final AccountManagerFuture<Bundle> future = mAccountManager
.getAuthToken(account, authTokenType, null, true,
null, handler);
单击允许按钮 returns 正确 AuthToken
。但是我想在调用 getAuthToken
时看到授予权限页面(上图),而不是通过单击通知。我该怎么做?
这里有几件事要做。在 Android 中使用线程通常被认为是不好的做法,根据 Android 文档,建议使用异步任务或处理程序。现在对于每个 Android 文档的 Auth 消息,预期输出是一个通知。
getAuthToken(Account account, String authTokenType, Bundle options, boolean notifyAuthFailure, AccountManagerCallback<Bundle> callback, Handler handler)
Gets an auth token of the specified type for a particular account,
optionally raising a notification if the user must enter credentials.
注意到 getAuthToken 有一个 Handler 参数了吗?这将是处理任务异步的首选方法。这里的问题是 you CAN NOT have a full screen message on a handler thread, because it can't interrupt the UI thread.
在您的第一个示例中,您实际上在 UI 线程上调用了 call mAccountManager
,因此它允许它接管 UI 并发送全屏允许或拒绝消息,但是这不能用处理程序完成,因为处理程序不能使用 UI 线程(将在运行时抛出错误)。
我提出的解决方案?如果您想要全屏中断消息,请不要使用处理程序,在 UI 线程上执行操作,类似于您的第一个代码片段。
AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, authTokenType, null, this, callback, null);
//USE implements and implement a listener in the class declaration and
//use 'this' in the callback param OR create a new callback method for it
我使用了这个方法而不是以前的方法,现在我看到了确认对话框:
accountManager.getAuthToken(account, AUTH_TOKEN_TYPE_FULL_ACCESS, null, true, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle bundle = future.getResult();
String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
} catch (OperationCanceledException | IOException | AuthenticatorException e) {
}
}
}, null);
请注意,第二个应用必须具有不同的签名。如果两个应用程序具有相同的签名,则不需要确认并且 authToken
将检索。
我有两个使用相同帐户类型的应用程序。当用户第一次打开第二个应用程序并且存在一个帐户时,我希望显示以下页面:
但是当我运行这个代码时没有任何反应:
final AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, authTokenType, null, this, null, null);
new Thread(new Runnable() {
@Override
public void run() {
try {
Bundle bnd = future.getResult();
final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
showMessage((authtoken != null) ? "SUCCESS!\ntoken: " + authtoken : "FAIL");
Log.d("udinic", "GetToken Bundle is " + bnd);
} catch (Exception e) {
e.printStackTrace();
showMessage(e.getMessage());
}
}
}).start();
当我从具有身份验证器的应用程序 运行 上面的代码时,它可以正常工作。当我 运行 下面的代码改为时,系统会生成一个通知,当我点击它时,会出现上面的图片。
final AccountManagerFuture<Bundle> future = mAccountManager
.getAuthToken(account, authTokenType, null, true,
null, handler);
单击允许按钮 returns 正确 AuthToken
。但是我想在调用 getAuthToken
时看到授予权限页面(上图),而不是通过单击通知。我该怎么做?
这里有几件事要做。在 Android 中使用线程通常被认为是不好的做法,根据 Android 文档,建议使用异步任务或处理程序。现在对于每个 Android 文档的 Auth 消息,预期输出是一个通知。
getAuthToken(Account account, String authTokenType, Bundle options, boolean notifyAuthFailure, AccountManagerCallback<Bundle> callback, Handler handler)
Gets an auth token of the specified type for a particular account, optionally raising a notification if the user must enter credentials.
注意到 getAuthToken 有一个 Handler 参数了吗?这将是处理任务异步的首选方法。这里的问题是 you CAN NOT have a full screen message on a handler thread, because it can't interrupt the UI thread.
在您的第一个示例中,您实际上在 UI 线程上调用了 call mAccountManager
,因此它允许它接管 UI 并发送全屏允许或拒绝消息,但是这不能用处理程序完成,因为处理程序不能使用 UI 线程(将在运行时抛出错误)。
我提出的解决方案?如果您想要全屏中断消息,请不要使用处理程序,在 UI 线程上执行操作,类似于您的第一个代码片段。
AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(account, authTokenType, null, this, callback, null);
//USE implements and implement a listener in the class declaration and
//use 'this' in the callback param OR create a new callback method for it
我使用了这个方法而不是以前的方法,现在我看到了确认对话框:
accountManager.getAuthToken(account, AUTH_TOKEN_TYPE_FULL_ACCESS, null, true, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle bundle = future.getResult();
String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
} catch (OperationCanceledException | IOException | AuthenticatorException e) {
}
}
}, null);
请注意,第二个应用必须具有不同的签名。如果两个应用程序具有相同的签名,则不需要确认并且 authToken
将检索。