java 物质主义#23~25
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 物质主义#23~25相关的知识,希望对你有一定的参考价值。
@SuppressLint("MissingPermission")
public static Pair<String, String> getCredentials(Context context) {
String username = Preferences.getUsername(context);
if (TextUtils.isEmpty(username)) {
return null;
}
AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
for (Account account : accounts) {
if (TextUtils.equals(username, account.name)) {
return Pair.create(username, accountManager.getPassword(account));
}
}
return null;
}
@SuppressLint("MissingPermission")
public static void showLogin(Context context, AlertDialogBuilder alertDialogBuilder) {
Account[] accounts = AccountManager.get(context).getAccountsByType(BuildConfig.APPLICATION_ID);
if (accounts.length == 0) { // no accounts, ask to login or re-login
context.startActivity(new Intent(context, LoginActivity.class));
} else if (!TextUtils.isEmpty(Preferences.getUsername(context))) { // stale account, ask to re-login
context.startActivity(new Intent(context, LoginActivity.class));
} else { // logged out, choose from existing accounts to log in
showAccountChooser(context, alertDialogBuilder, accounts);
}
}
@SuppressLint("MissingPermission")
static void registerAccountsUpdatedListener(final Context context) {
AccountManager.get(context).addOnAccountsUpdatedListener(accounts -> {
String username = Preferences.getUsername(context);
if (TextUtils.isEmpty(username)) {
return;
}
for (Account account : accounts) {
if (TextUtils.equals(account.name, username)) {
return;
}
}
Preferences.setUsername(context, null);
}, null, true);
}
以上是关于java 物质主义#23~25的主要内容,如果未能解决你的问题,请参考以下文章
java 物质主义#26
java 物质主义#10〜#22
java 物质主义#09
java 物质主义#07
java 物质主义#05
java 物质主义#04