Last active
August 29, 2015 14:05
-
-
Save nein37/4ac94a219300ae3505af to your computer and use it in GitHub Desktop.
AccountManagerを利用する ref: http://qiita.com/nein37/items/25ba6e5176118fae5f13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.GET_ACCOUNTS"/> | |
<uses-permission android:name="android.permission.USE_CREDENTIALS"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AccountManager manager = AccountManager.get(context); | |
Account[] accountArray = manager.getAccounts(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AccountManager manager = AccountManager.get(this); | |
manager.getAuthToken(new Account("user@gmail.com", "com.google"), "mail", null, | |
this, new AccountManagerCallback<Bundle>() { | |
@Override | |
public void run(AccountManagerFuture<Bundle> future) { | |
Bundle bundle = null; | |
try { | |
bundle = future.getResult(); | |
String accountName = bundle.getString(AccountManager.KEY_ACCOUNT_NAME); | |
String accountType = bundle.getString(AccountManager.KEY_ACCOUNT_TYPE); | |
String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN); | |
Toast.makeText(MyActivity.this,authToken,Toast.LENGTH_SHORT).show(); | |
} catch (OperationCanceledException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (AuthenticatorException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AccountManager manager = AccountManager.get(this); | |
manager.invalidateAuthToken("com.google",TOKEN); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AccountManager manager = AccountManager.get(context); | |
Account[] accountArray = manager.getAccountsByType("com.google"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the account list, and pick the first one | |
final String ACCOUNT_TYPE_GOOGLE = "com.google"; | |
final String[] FEATURES_MAIL = { | |
"service_mail" | |
}; | |
AccountManager.get(this).getAccountsByTypeAndFeatures(ACCOUNT_TYPE_GOOGLE, FEATURES_MAIL, | |
new AccountManagerCallback() { | |
@Override | |
public void run(AccountManagerFuture future) { | |
Account[] accounts = null; | |
try { | |
accounts = future.getResult(); | |
if (accounts != null && accounts.length > 0) { | |
String selectedAccount = accounts[0].name; | |
queryLabels(selectedAccount); | |
} | |
ru | |
} catch (OperationCanceledException oce) { | |
// TODO: handle exception | |
} catch (IOException ioe) { | |
// TODO: handle exception | |
} catch (AuthenticatorException ae) { | |
// TODO: handle exception | |
} | |
} | |
}, null /* handler */); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onClick(View v) { | |
Intent intent = AccountManager.get(this).newChooseAccountIntent(null, null, new String[] { | |
"com.google" | |
}, false, null, | |
null, null, null); | |
startActivityForResult(intent, REQUEST_CODE); | |
} | |
@Override | |
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
{ | |
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) | |
Toast.makeText(this, data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME), | |
Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AccountManager manager = AccountManager.get(this); | |
manager.addAccount("com.google", "mail", null, null, activity, | |
new AccountManagerCallback<Bundle>() { | |
@Override | |
public void run(AccountManagerFuture<Bundle> future) { | |
try { | |
Bundle bundle = future.getResult(); | |
String accountName = bundle.getString(AccountManager.KEY_ACCOUNT_NAME); | |
} catch (OperationCanceledException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (AuthenticatorException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AccountManager manager = AccountManager.get(this); | |
manager.removeAccount(new Account("user@gmail.com","com.google"), | |
new AccountManagerCallback<Boolean>() { | |
@Override | |
public void run(AccountManagerFuture<Boolean> future) { | |
try { | |
boolean result = future.getResult(); | |
} catch (OperationCanceledException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (AuthenticatorException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AccountManager manager = AccountManager.get(this); | |
manager.clearPassword(new Account("user@gmail.com","com.google")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment