2

我想验证用户名和密码是否有效(例如 Google 帐户、Facebook 帐户、Twitter 帐户...)。我在 AccountManager 类中找到了confirmCredentials API。我可以使用此 API 来验证帐户的密码吗?
我编写了这个checkCredentials API 来检查用户的密码是否有效,但这会导致 ANR 并且我无法得到密码是否有效的结果?
你有关于这个API或者如何在Andoroid下验证id和密码的经验吗?
任何信息或线索将不胜感激。非常感谢。

4

1 回答 1

1

我在我的应用程序中使用了 confirmCredentials。我在下面放了一些示例代码以防万一...

public class MyActivity implements AccountManagerCallback<Bundle>

<snip>

    // Get the user to confirm their credentials 
    Account account = new Account("username", "com.mydomain.myapp");

    // The result of this call is handled in the
    // run(AccountManagerFuture<Bundle> response) method below
    AccountManager.get(this).confirmCredentials(account,
                                                null,
                                                this,
                                                this,
                                                null);

<snip>

  /**
   * Handle callbacks from the {@link AccountManager} methods
   */
  @Override
  public void run(AccountManagerFuture<Bundle> response)
  {
    try
    {
      // This call blocks while waiting for result from confirmCredentials
      Bundle result = response.getResult();

      // Handle the result
    }
    catch (OperationCanceledException e)
    {
      // User cancelled login
      e.printStackTrace();
    }
    catch (AuthenticatorException e)
    {
      // Failed to login
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    finally
    {
      // Always exit
      finish();
    }
  }
于 2011-10-24T18:29:06.190 回答