Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AccountAuthenticatorService extends Service {
- private static final String LOG_TAG = AccountAuthenticatorService.class.getSimpleName();
- private static AccountAuthenticatorImpl sAccountAuthenticator = null;
- public AccountAuthenticatorService() {
- super();
- }
- @Override
- public IBinder onBind(Intent intent) {
- IBinder ret = null;
- if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT)) {
- ret = getAuthenticator().getIBinder();
- }
- return ret;
- }
- public AbstractAccountAuthenticator getAuthenticator() {
- if (AccountAuthenticatorService.sAccountAuthenticator == null) {
- AccountAuthenticatorService.sAccountAuthenticator = new AccountAuthenticatorImpl(this);
- }
- return AccountAuthenticatorService.sAccountAuthenticator;
- }
- private static class AccountAuthenticatorImpl extends AbstractAccountAuthenticator {
- private Context mContext;
- public AccountAuthenticatorImpl(Context context) {
- super(context);
- mContext = context;
- }
- @Override
- public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
- return null;
- }
- // When adding a new account from the cell phone, this window opens
- @Override
- public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
- Intent add = new Intent(mContext, AccountManagerSimpleActivity.class);
- add.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
- Bundle reply = new Bundle();
- reply.putParcelable(AccountManager.KEY_INTENT, add);
- return reply;
- }
- @Override
- public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
- return null;
- }
- @Override
- public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
- AccountManager am = AccountManager.get(mContext);
- String user = account.name;
- String pass = am.getPassword(account);
- String token = "sample-no-token";
- Bundle result = new Bundle();
- result.putString(AccountManager.KEY_ACCOUNT_NAME, user);
- result.putString(AccountManager.KEY_ACCOUNT_TYPE, "com.google");
- result.putString(AccountManager.KEY_AUTHTOKEN, token);
- return result;
- }
- @Override
- public String getAuthTokenLabel(String authTokenType) {
- return null;
- }
- @Override
- public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
- return null;
- }
- @Override
- public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
- return null;
- }
- }
- }
- // Button to execute the login from gmail account
- registration.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String email = "com.google";
- try{
- Intent googlePicker = AccountManager.newChooseAccountIntent(null, null,
- new String[]{email}, true, null, null, null, null);
- startActivityForResult(googlePicker, PICK_ACCOUNT_REQUEST);
- } catch (ActivityNotFoundException ex) {
- Toast.makeText(AccountManagerSimpleActivity.this, "Error de sincronización en las cuentas: " + ex.getMessage(), Toast.LENGTH_LONG);
- }
- }
- });
- cancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- }
- // Function that complements the registry by gmail
- @SuppressLint("WrongConstant")
- @Override
- protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
- if (requestCode == PICK_ACCOUNT_REQUEST && resultCode == RESULT_OK) {
- String useremail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
- Toast.makeText(this,"Cuenta sincronizada con éxito",3000).show();
- }else{
- Toast.makeText(this,"No hay cuentas sincronizadas",3000).show();
- }
- }
Add Comment
Please, Sign In to add comment